日期:2014-05-20 浏览次数:20902 次
public static class __9_7_Person implements Comparator<__9_7_Person> { private int height; private int weight; public __9_7_Person(int height, int weight) { this.height = height; this.weight = weight; } public int compare(__9_7_Person p1, __9_7_Person p2) { if (p1.height != p2.height) { return p1.height - p2.height; } else { return p1.weight - p2.weight; } } }
__9_7_Person p[] = {new __9_7_Person(60, 100), new __9_7_Person(70, 150), new __9_7_Person(56, 90), new __9_7_Person(75, 190), new __9_7_Person(60, 95), new __9_7_Person(68, 110), };
package sortutil.test; import java.util.Arrays; import java.util.Comparator; public class __9_7_Person implements Comparator<__9_7_Person> { private int height; private int weight; public __9_7_Person(int height, int weight) { this.height = height; this.weight = weight; } public int compare(__9_7_Person p1, __9_7_Person p2) { if (p1.height != p2.height) { return p1.height - p2.height; } else { return p1.weight - p2.weight; } } public String toString(){ return "\nHeigth:"+height +"||"+"Weight:"+weight; } public static void main(String[] args) { __9_7_Person[] p = {new __9_7_Person(60, 100), new __9_7_Person(70, 150), new __9_7_Person(56, 90), new __9_7_Person(75, 190), new __9_7_Person(60, 95), new __9_7_Person(68, 110), }; Arrays.sort(p,new __9_7_Person(0,0));//new __9_7_Person(0,0)被转成comparator System.out.println(Arrays.toString(p)); } }