Before I start, let me ask you a question: we now have an Employee class.
@Data @AllArgsConstructor public class Employee { private Integer id; private Integer age; //Age private String gender; //Gender private String firstName; private String lastName; }
Do you know how to sort a List set of Employee objects in reverse order of gender field and then age? If you don't know the solution within 4 lines of code (in fact, 1 line of code can be implemented, but the author formats it as 4 lines), I think you need to look at it step by step.
1, String List sort
cities is an array of strings. Notice that london's initial is lowercase.
List<String> cities = Arrays.asList( "Milan", "london", "San Francisco", "Tokyo", "New Delhi" ); System.out.println(cities); //[Milan, london, San Francisco, Tokyo, New Delhi] cities.sort(String.CASE_INSENSITIVE_ORDER); System.out.println(cities); //[london, Milan, New Delhi, San Francisco, Tokyo] cities.sort(Comparator.naturalOrder()); System.out.println(cities); //[Milan, New Delhi, San Francisco, Tokyo, london]
- When using the sort method, follow the String.CASE_ INSENSITIVE_ The rule sort of order (case insensitive) results in: [london, Milan, New Delhi, San Francisco, Tokyo]
- If using Comparator.naturalOrder() natural alphabetical order, the result is: [Milan, New Delhi, San Francisco, Tokyo, london]
Similarly, we can use the sorter Comparator in the Stream pipeline flow.
cities.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println); //Milan //New Delhi //San Francisco //Tokyo //london
In java 7 we use Collections.sort() accepts an array parameter to sort the array. After java 8, you can directly call the sort() method of the collection class to sort. The parameter of the sort() method is an implementation class of the Comparator interface, which we will introduce in the next section.
2, Integer type List sort
List<Integer> numbers = Arrays.asList(6, 2, 1, 4, 9); System.out.println(numbers); //[6, 2, 1, 4, 9] numbers.sort(Comparator.naturalOrder()); //Natural order System.out.println(numbers); //[1, 2, 4, 6, 9] numbers.sort(Comparator.reverseOrder()); //Reverse order System.out.println(numbers); //[9, 6, 4, 2, 1]
3, Sort list < Object > by object field
This function is more interesting. For example, let's understand.
Employee e1 = new Employee(1,23,"M","Rick","Beethovan"); Employee e2 = new Employee(2,13,"F","Martina","Hengis"); Employee e3 = new Employee(3,43,"M","Ricky","Martin"); Employee e4 = new Employee(4,26,"M","Jon","Lowman"); Employee e5 = new Employee(5,19,"F","Cristine","Maria"); Employee e6 = new Employee(6,15,"M","David","Feezor"); Employee e7 = new Employee(7,68,"F","Melissa","Roy"); Employee e8 = new Employee(8,79,"M","Alex","Gussin"); Employee e9 = new Employee(9,15,"F","Neetu","Singh"); Employee e10 = new Employee(10,45,"M","Naveen","Jain"); List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); employees.sort(Comparator.comparing(Employee::getAge)); employees.forEach(System.out::println);
- First, we created 10 Employee objects and then converted them to lists
- Then the key code: using the function to apply Employee::getAge as the sorting field of the object, that is, using the age of the employee as the sorting field
- Then we call the forEach method of List to print the result of List sorting as follows. (of course, we rewrite the toString method of Employee, otherwise the printing result is meaningless):
Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis) Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor) Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh) Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria) Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan) Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman) Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin) Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain) Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy) Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin)
- If we want the List to be sorted in reverse order of age, use the reversed() method. For example:
employees.sort(Comparator.comparing(Employee::getAge).reversed());
4, The Comparator chain sorts list < Object >
The following code is sorted in reverse order by gender and then by age.
employees.sort( Comparator.comparing(Employee::getGender) .thenComparing(Employee::getAge) .reversed() ); employees.forEach(System.out::println); //It's all positive, not reversed //All in reverse order, followed by a reserved //First in reverse order (plus reserved), then in positive order //First, positive order (plus reserved), then reverse order (plus reserved)
Careful friends may notice that we only use a reversed() reverse method, which is not the same as SQL. This problem is not easy to describe in words. I suggest you take a look at it Video corresponding to this article!
The sorting results are as follows:
Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin) Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain) Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin) Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman) Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan) Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor) Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy) Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria) Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh) Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis)
Welcome to my blog, which has a lot of collections
- Reprint of this article indicates the source (it must be connected, not just the text): Alphabet blog.
If you think it will help you, please give me some praise and share! Your support is my endless creative power. In addition, the author recently output the following excellent content, looking forward to your attention.