Java
naturalOrder是比较器功能接口的静态方法。
Java 8中引入的Comparator.naturalOrder方法返回一个比较器,该比较器以自然顺序比较可比较对象。
对于自然排序,一个类需要实现Comparable并定义compareTo方法。
一个对象的集合根据compareTo方法以自然排序进行排序。
像Integer、String和Date这样的Java类实现了Comparable接口并覆盖了其compareTo方法,它们以词表顺序(lexicographic-order)进行排序。
从Java源代码中找到naturalOrder方法声明。

  1. static <T extends Comparable<? super T>> Comparator<T> naturalOrder()

为了扭转自然排序,可以使用Comparator.reverseOrder方法。
提供一些例子,说明如何使用Comparator.naturalOrderStream.sortedCollections.sortList.sortArrays.sort一起对对象的集合进行自然排序。

Stream.sorted 一起使用

Stream.sorted返回一个由这个流的元素组成的流,根据提供的比较器进行排序。
StreamSortedDemo.java

  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. public class StreamSortedDemo {
  5. public static void main(String[] args) {
  6. List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
  7. numList.stream().sorted(Comparator.naturalOrder()).forEach(n -> System.out.print(n + " "));
  8. System.out.println("\n-----------");
  9. List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
  10. strList.stream().sorted(Comparator.naturalOrder()).forEach(s -> System.out.print(s + " "));
  11. System.out.println("\n-----------");
  12. List<Student> stdList = Student.getStudentList();
  13. stdList.stream().sorted(Comparator.naturalOrder()).forEach(s -> System.out.print(s.getName() + " "));
  14. System.out.println("\n-----------");
  15. //reverse order of natural order using Comparator.reverseOrder()
  16. stdList = Student.getStudentList();
  17. stdList.stream().sorted(Comparator.reverseOrder()).forEach(s -> System.out.print(s.getName() + " "));
  18. }
  19. }

Student.java

  1. import java.util.Arrays;
  2. import java.util.List;
  3. public class Student implements Comparable<Student> {
  4. private String name;
  5. private int age;
  6. public Student(String name, int age) {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. @Override
  17. public int compareTo(Student s) {
  18. return name.compareTo(s.getName());
  19. }
  20. public static List<Student> getStudentList() {
  21. Student s1 = new Student("Ram", 18);
  22. Student s2 = new Student("Shyam", 22);
  23. Student s3 = new Student("Mohan", 19);
  24. Student s4 = new Student("Mahesh", 20);
  25. Student s5 = new Student("Krishna", 21);
  26. List<Student> list = Arrays.asList(s1, s2, s3, s4, s5);
  27. return list;
  28. }
  29. }

输出

  1. 8 10 11 12 15
  2. -----------
  3. Allahabad Kanpur Noida Varanasi
  4. -----------
  5. Krishna Mahesh Mohan Ram Shyam
  6. -----------
  7. Shyam Ram Mohan Mahesh Krish

Collections.sort 一起使用

Collections.sort根据给定的比较器实例对指定的列表进行排序。
CollectionsSortDemo.java

  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. public class CollectionsSortDemo {
  6. public static void main(String[] args) {
  7. List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
  8. Collections.sort(numList, Comparator.naturalOrder());
  9. numList.forEach(n -> System.out.print(n + " "));
  10. System.out.println("\n-----------");
  11. List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
  12. Collections.sort(strList, Comparator.naturalOrder());
  13. strList.forEach(s -> System.out.print(s + " "));
  14. System.out.println("\n-----------");
  15. List<Student> stdList = Student.getStudentList();
  16. Collections.sort(stdList, Comparator.naturalOrder());
  17. stdList.forEach(s -> System.out.print(s.getName() + " "));
  18. }
  19. }

List.sort 一起使用

List.sort根据给定的比较器实例对这个列表进行排序。
ListSortDemo.java

  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. public class ListSortDemo {
  5. public static void main(String[] args) {
  6. List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
  7. numList.sort(Comparator.naturalOrder());
  8. numList.forEach(n -> System.out.print(n + " "));
  9. System.out.println("\n-----------");
  10. List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
  11. strList.sort(Comparator.naturalOrder());
  12. strList.forEach(s -> System.out.print(s + " "));
  13. System.out.println("\n-----------");
  14. List<Student> stdList = Student.getStudentList();
  15. stdList.sort(Comparator.naturalOrder());
  16. stdList.forEach(s -> System.out.print(s.getName() + " "));
  17. }
  18. }

Arrays.sort 一起使用

Arrays.sort根据指定的比较器引起的顺序对指定的对象阵列进行排序。
ArraysSortDemo.java

  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. public class ArraysSortDemo {
  4. public static void main(String[] args) {
  5. Student s1 = new Student("Ram", 18);
  6. Student s2 = new Student("Shyam", 22);
  7. Student s3 = new Student("Mohan", 19);
  8. Student[] stdArray = { s1, s2, s3 };
  9. Arrays.sort(stdArray, Comparator.naturalOrder());
  10. for (Student s : stdArray) {
  11. System.out.print(s.getName() + " ");
  12. }
  13. }
  14. }