日期:2014-05-20 浏览次数:20821 次
public class TCharNum { public static void sort(int num[][]) { for (int i = 0; i < num.length; i++) { for (int j = i + 1; j < num.length; j++) { if (num[j][0] < num[i][0]) { int temp = num[j][0]; num[j][0] = num[i][0]; num[i][0] = temp; } else if (num[j][0] == num[i][0]) { if (num[j][1] < num[i][1]) { int temp = num[j][1]; num[j][1] = num[i][1]; num[i][1] = temp; } } } } } public static void main(String[] args) { int num[][] = {{600,22},{649,43},{700,34},{580,18},{700,25},{700,21},{649,21},{700,1}}; sort(num); for (int i = 0; i < num.length; i++) { System.out.println(num[i][0] + " " + num[i][1]); } } }
------解决方案--------------------
public class Demo { private double point; private int age; public Demo(double point,int age){ this.point=point; this.age=age; } public double getPoint() { return point; } public void setPoint(double point) { this.point = point; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } import java.util.Arrays; import java.util.Comparator; public class Test { public static void main(String[] arg) { Demo[] demo=new Demo[7]; demo[0]=new Demo(620,20); demo[1]=new Demo(649,43); demo[2]=new Demo(700,34); demo[3]=new Demo(580,18); demo[4]=new Demo(700,25); demo[5]=new Demo(700,21); demo[6]=new Demo(649,21); Arrays.sort(demo,new Comparator<Demo>(){ public int compare(Demo o1, Demo o2) { if(o1.getPoint()<o2.getPoint()) return 1; else if(o1.getPoint()==o2.getPoint()){ if(o1.getAge()<o2.getAge()){ return 1; }else if(o1.getAge()==o2.getAge()){ return 0; }else return -1; }else return -1; } }); for(Demo d:demo){ System.out.println(d.getPoint()+" "+d.getAge()); } } }