JDK1.8之前这样做;
//创建bean
public class Student {
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;
}public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
}//测试
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(40, "张三"));
list.add(new Student(18, "小强"));
list.add(new Student(30, "王强"));Collections.sort(list, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
// 按照年龄进行升序排列
if (s1.getAge() > s2.getAge()) {
return 1;
}
if (s1.getAge() == s2.getAge()) {
return 0;
}
return -1;
}
});
System.out.println("排序后的结果:" + list);}
排序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]]
也可以这样;
//创建bean
public class Student implements Comparable{
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;
}public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int compareTo(Object o) {
Student student = (Student)o;
if(this.age > student.age){
return 1;
}
if(this.age < student.age){
return -1;
}
return 0;
}
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
}//创建测试类
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(40, "张三"));
list.add(new Student(18, "小强"));
list.add(new Student(30, "王强"));
System.out.println("排序前的结果:" + list);
Collections.sort(list);
System.out.println("排序后的结果:" + list);
}
排序前的结果:[Student [age=40, name=张三], Student [age=18, name=小强], Student [age=30, name=王强]]
排序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]]
JDK1.8之后这样做;
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(40, "张三"));
list.add(new Student(18, "小强"));
list.add(new Student(30, "王强"));list.sort(new Comparator<Student>() {
public int compare(Student s1, Student s2) {
// 按照年龄进行升序排列
if (s1.getAge() > s2.getAge()) {
return 1;
}
if (s1.getAge() == s2.getAge()) {
return 0;
}
return -1;
}
});
System.out.println("排序后的结果:" + list);
}
排序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]]
还可以这样做;
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(40, "张三"));
list.add(new Student(18, "小强"));
list.add(new Student(30, "王强"));
list.sort(Comparator.comparing(Student::getAge));
System.out.println("升序后的结果:" + list);
list.sort(Comparator.comparing(Student::getAge).reversed());
System.out.println("降序后的结果:" + list);
}
升序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]]
降序后的结果:[Student [age=40, name=张三], Student [age=30, name=王强], Student [age=18, name=小强]]