在编写程序时,不可避免的一个问题就是排序。在Java代码中如何对一个集合进行排序?我们今天就整理一个Java中用于排序的工具。

1.原子类型数组排序

对于原子类型数组排序,Arrays.sort。且只能由小到大的排序。无法使用Comparator

  1. Arrays.sort(array);

2.引用类型数组排序

引用类型数组或者集合排序,就可以使用Comparator

  1. Integer[] array = {3,2,6,1,7,2,78,11};
  2. Arrays.sort(array, (x,y)->y-x);
  3. //结果:[78, 11, 7, 6, 3, 2, 2, 1]
  4. System.out.println(Arrays.toString(array));
  1. Comparator需要重写一个,2个入参和返回1个整型的结果
  2. 整数的正、负代表2个数是否调换位置,y是x后面的数

    • +: 说明需要调换,y,x
    • -: 说明不需要调换 x,y
    • 0: 也不用调换

      3.引用类型集合排序

      1. List<Integer> integers = Arrays.asList(3, 2, 6, 1, 7, 2, 78, 11);
      2. Collections.sort(integers);
      3. //[1, 2, 2, 3, 6, 7, 11, 78]
      4. System.out.println(integers);
      5. Collections.sort(integers,(x,y)->y-x);
      6. //[78, 11, 7, 6, 3, 2, 2, 1]
      7. System.out.println(integers);

      4.Comparator的用法

      前面的一些演示,主要目的是说,Comparator一般放在哪些地方。下面具体演示一些用法
  3. 手写lamda表达式法

    1. List<Integer> integers = Arrays.asList(3, 2, 6, 1, 7, 2, 78, 11);
    2. Collections.sort(integers,(x,y)->y-x);
    3. //[78, 11, 7, 6, 3, 2, 2, 1]
    4. System.out.println(integers);
  4. 提取可比较字段类型比较

    1. class People {
    2. private Integer id;
    3. private String name;
    4. private Integer age;
    5. public People(Integer id, String name, Integer age) {
    6. this.id = id;
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public Integer getAge() {
    11. return age;
    12. }
    13. @Override
    14. public String toString() {
    15. return "People{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}';
    16. }
    17. }
    18. @Test
    19. public void testComparing() {
    20. List<People> people = Arrays.asList(new People(1, "高溪", 23),
    21. new People(2, "James", 37),
    22. new People(3, "Kobe", 32),
    23. new People(4, "WY", 25));
    24. Collections.sort(people,Comparator.comparing(People::getAge));
    25. System.out.println(people);
    26. }
    • 按照People类的age字段进行排序
    • Comparator.comaring(),提供一个排序的字段
  5. int、double等确定排序字段类型

    1. class People {
    2. private Integer id;
    3. private String name;
    4. private Integer age;
    5. public People(Integer id, String name, Integer age) {
    6. this.id = id;
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public Integer getAge() {
    11. return age;
    12. }
    13. public Integer getId() {
    14. return id;
    15. }
    16. public String getName() {
    17. return name;
    18. }
    19. @Override
    20. public String toString() {
    21. return "People{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}';
    22. }
    23. }
    24. @Test
    25. public void testComparing() {
    26. List<People> people = Arrays.asList(new People(1, "高溪", 23),
    27. new People(3, "James", 37),
    28. new People(2, "Kobe", 37),
    29. new People(4, "WY", 25));
    30. Collections.sort(people,Comparator.comparingInt(People::getAge));
    31. System.out.println(people);
    32. }
    • 排序字段age是Integer
    • 直接可以指定按照int排序
  6. 按照多个字段排序

    1. List<People> people = Arrays.asList(new People(1, "高溪", 23),
    2. new People(3, "James", 37),
    3. new People(2, "Kobe", 37),
    4. new People(4, "WY", 25));
    5. Collections.sort(people,Comparator.comparingInt(People::getAge).thenComparing(Comparator.comparingInt(People::getId)));
    6. System.out.println(people);
    • age如果相同按照id排序
  7. null字段处理

    1. List<People> people = Arrays.asList(new People(1, "高溪", 23),
    2. new People(3, "James", 37),
    3. new People(2, "Kobe", null),
    4. new People(4, "WY", 25));
    5. //fixme: java.lang.NullPointerException
    6. Collections.sort(people,Comparator.comparing(People::getAge));
    7. System.out.println(people);
    • 如果字段有null值,会报错

      1. List<People> people = Arrays.asList(new People(1, "高溪", 23),
      2. new People(3, "James", 37),
      3. new People(2, "Kobe", null),
      4. new People(4, "WY", 25));
      5. Collections.sort(people,Comparator.comparing(People::getAge,Comparator.nullsFirst(Comparator.naturalOrder())));
      6. System.out.println(people);
    • 先指定排序字段

    • nullFisrt中指定的是对 所提取字段的排序方式。

      1. List<People> people = Arrays.asList(new People(1, "高溪", 23),
      2. new People(3, "James", 37),
      3. new People(2, "Kobe", null),
      4. new People(4, "WY", 25));
      5. Collections.sort(people,Comparator.comparing(People::getAge,Comparator.nullsFirst(Comparator.comparingInt(x->-x))));
      6. System.out.println(people);
    • nullFirst中的比较器是提取字段的值再提取

  8. 自然排序和逆序排序

    1. List<People> people = Arrays.asList(new People(1, "高溪", 23),
    2. new People(3, "James", 37),
    3. new People(2, "Kobe", 34),
    4. new People(4, "WY", 25));
    5. Collections.sort(people,Comparator.comparing(People::getAge,Comparator.reverseOrder()));
    6. System.out.println(people);
    7. Collections.sort(people,Comparator.comparing(People::getAge,Comparator.naturalOrder()));
    8. System.out.println(people);
    • 提取字段一定是可排序字段, 实现了Comparable
    • 默认是自然排序
    • 可以通过调用Comparator.reverseOrder()实现逆序。