publicclassTest1{publicstaticvoidmain(String[] args){Person[] people =newPerson[3];
people[0]=newPerson(5,"daw");
people[1]=newPerson(1,"weqw");
people[2]=newPerson(3,"3");Arrays.sort(people,newComparator<Person>(){@Overridepublicintcompare(Person person,Person t1){return(person.getId()-person.getId());}});System.out.println(Arrays.toString(people));}}
按double类型的身高排序
使用包装类Double自带的比较函数
publicclassTest1{publicstaticvoidmain(String[] args){Person[] people =newPerson[3];
people[0]=newPerson(5,"daw",155.0);
people[1]=newPerson(1,"weqw",155.1);
people[2]=newPerson(3,"3",195.0);Arrays.sort(people,newComparator<Person>(){@Overridepublicintcompare(Person person,Person t1){returnDouble.compare(person.getHeight(),t1.getHeight());}});for(int i =0; i < people.length; i++){System.out.println("this is "+(i+1)+"th person named:"+ people[i].getId()+"and his or her height is"+people[i].getHeight());}}}