在Java中,可以使用List集合对象的sort(Comparator super E> c)方法,按照对象中某字段对List集合进行排序
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
栗子:
import java.util.*;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List<Person2> personList = new ArrayList<Person2>();
personList.add(new Person2("Tom", 8900, 18,"male", "A"));
personList.add(new Person2("Jack", 7000, 19,"male", "A"));
personList.add(new Person2("Lily", 7800, 20,"female", "D"));
personList.add(new Person2("Anni", 8200, 18,"female", "B"));
personList.add(new Person2("Owen", 9500, 23,"male", "D"));
personList.add(new Person2("Alisa", 7900, 28,"female", "C"));
//根据List集合对象的sort方法(语句后x.get与y.get调换位置即为倒序)
personList.sort((x,y) -> Integer.compare(x.getSalary(),y.getSalary()));//int类型正序
personList.forEach(System.out::println);
System.out.println("================================");
personList.sort((a, b) -> a.getSalary() - b.getSalary());//int类型正序
personList.forEach(System.out::println);
System.out.println("================================");
personList.sort(Comparator.comparing(Person2::getSalary));//int类型正序排序
personList.forEach(System.out::println);
System.out.println("================================");
personList.sort((a,b) -> a.getLevel().compareTo(b.getLevel()));//其他类型正序
personList.forEach(System.out::println);
System.out.println("================================");
//stream流的sorted方法正序排序(倒序:Comparator.comparing(Person2::getSalary).reversed())
personList = personList.stream().sorted(Comparator.comparing(Person2::getSalary)).collect(Collectors.toList());
personList.forEach(System.out::println);
System.out.println("================================");
}
}
class Person2 {
private String name; // 姓名
private int salary; // 薪资
private int age; // 年龄
private String sex; //性别
private String level; // 级别
// 构造方法
public Person2(String name, int salary, int age, String sex, String level) {
this.name = name;
this.salary = salary;
this.age = age;
this.sex = sex;
this.level = level;
}
// 省略了get和set,请自行添加
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "Person2{" +
"name=" + name +
", salary='" + salary + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
", level=" + level +
'}';
}
}
打印输出结果如下:
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================