目标:实现对Person 对象的年龄,从小到大排序
1、实现排序
package com.app; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class A1 { public static void main(String[] args) {
List<Person> list = getData() ;
//排序
Collections.sort( list , comparator ); for (Person person : list) {
System.out.println("person " + person.getName() + " " + person.getAge() );
} } /**
* 年龄按从小到大排序
*/
static Comparator<Person> comparator = new Comparator<Person>() {
@Override
public int compare(Person p1 , Person p2 ) {
if( p1.getAge() > p2.getAge() ){
return 1 ; //正数
}else if ( p1.getAge() < p2.getAge()) {
return -1 ; //负数
}else {
return 0; //相等为0
}
}
}; static private List<Person> getData() {
List<Person> list = new ArrayList<>() ; Person p1 = new Person() ;
p1.setAge( 10 );
p1.setName( "p1" ); Person p2 = new Person() ;
p2.setAge( 30 );
p2.setName( "p2" ); Person p3 = new Person() ;
p3.setAge( 20 );
p3.setName( "p3" ); Person p4 = new Person() ;
p4.setAge( 15 );
p4.setName( "p4" ); list.add( p1 ) ;
list.add( p2 ) ;
list.add( p3 ) ;
list.add( p4 ) ; return list ;
}
}
2、创建Person类
package com.app; public class Person { private int age ;
private String name ; public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、运行结果(实现对年龄的排序)
person p1 10
person p4 15
person p3 20
person p2 30