list.stream().sorted() Java8 Stream’s sorted() sorting. Forward order, reverse order, multi-field sorting

For collection sorting, java8 can use sorted() of the Stream stream for sorting.

ExampleBeans
We will use this Bean as an example below.

public class Order {<!-- -->

    private String weight;

    private Double price;

    private String dateStr;

    //Ignore getter, setter, constructor, toString
}

Field sorting
The first is the comparator Comparator, which has the following form:

Comparator<class name of object> comparator = Comparator.comparing(class name of object::get method name, ascending or descending order)

1. Commonly used APIs for sorting:

naturalOrder() represents natural sorting (usually ascending order). The natural order of numbers is numerical order, strings are sorted in alphabetical order, and dates are sorted in chronological order.
reverseOrder() means descending order.
nullsLast() means that if the attribute is null, put it at the end.
nullsFirst() means that if the attribute is null, put it at the front.

Ascending order is as follows:
naturalOrder() represents natural sorting (usually ascending order). It does not handle the null value of the attribute. Null pointers may be generated during sorting.

Comparator<Order> comparator1 = Comparator.comparing(Order::getDateStr, Comparator.naturalOrder());
List<Order> orderList1 = list.stream().sorted(comparator1).collect(Collectors.toList());
System.out.println("orderList1:" + orderList1);

Descending order is as follows:
Comparator.reverseOrder() represents descending order and does not handle the null value of the attribute. Null pointers may be obtained during sorting.

Comparator<Order> comparator2 = Comparator.comparing(Order::getDateStr, Comparator.reverseOrder());
List<Order> orderList2 = list.stream().sorted(comparator2).collect(Collectors.toList());
System.out.println("orderList2:" + orderList2);

Comparator.nullsLast() means that if the attribute is null, put it at the end

Comparator<Order> comparator3 = Comparator.comparing(Order::getDateStr, Comparator.nullsLast(naturalOrder()) );
List<Order> orderList3 = list.stream().sorted(comparator3).collect(Collectors.toList());
System.out.println("orderList3:" + orderList3);

Comparator.nullsFirst() means that if the property is null, put it at the front.

Comparator<Order> comparator4 = Comparator.comparing(Order::getDateStr, Comparator.nullsFirst(Comparator.reverseOrder()));
List<Order> orderList4 = list.stream().sorted(comparator4).collect(Collectors.toList());
System.out.println("orderList4:" + orderList4);

2.java8 multi-field sorting

Sometimes we also need to sort multiple fields.
To sort multiple fields, sort the first sort field first. When the first sort fields are the same, the second sort field will be used for sorting.

Multi-field sorting API
Format for sorting multiple fields

Comparator<class name of object> comparator = Comparator.comparing(class name of object::method name 1, ascending or descending order)
.thenComparing(Comparator.comparing(object’s class name::method name 2, ascending or descending order));

If there are more than three fields to sort, continue to use thenComparing to connect.

Examples are as follows:
First sort by the first field in descending order, then by the second field in descending order. If the attribute is null, put it at the end.

Comparator<Order> comparator5 = Comparator.comparing(Order::getDateStr, Comparator.nullsLast(Comparator.reverseOrder()))
        .thenComparing(Order::getPrice, Comparator.nullsLast(Comparator.reverseOrder()));
List<Order> orderList5 = list.parallelStream().sorted(comparator5).collect(Collectors.toList());
System.out.println("orderList5:" + orderList5);

The difference between reversed() and Comparator.reverseOrder():
Comparator.comparing(object class name::property method name).reversed();
Comparator.comparing(object class name::property method name, Comparator.reverseOrder());

reversed() is to reverse the sorted results after getting them.
Comparator.reverseOrder() sorts the properties in descending order.
reversed() can easily cause confusion when sorting multiple fields, so it is not recommended to use it.
Comparator.reverseOrder() is better understood and easier to use.

code show as below:

public class SortedDemo {<!-- -->

    /**
     * naturalOrder() represents natural sorting (usually ascending order),
     * Comparator.reverseOrder() means descending order.
     *
     * nullsLast() means that if the attribute is null, put it at the end.
     * nullsFirst() means that if the attribute is null, put it at the front.
     *
     */
    public static void order() {<!-- -->
        //Set the order object attributes. The following is how to write lombok, which is equivalent to setter.
        Order order1=Order.builder().dateStr("2021-10-01 00:00:00").price(33.3).build();
        Order order2=Order.builder().dateStr("2021-10-01 00:00:00").price(1.3).build();
        Order order3=Order.builder().dateStr("2021-10-01 00:00:00").price(55.3).build();
        Order order4=Order.builder().dateStr("2021-12-01 00:00:00").price(55.3).build();
        //Add null test
        Order order5=Order.builder().dateStr(null).price(null).build();

        List<Order> list= new ArrayList<>();
        list.add(order1);
        list.add(order2);
        list.add(order3);
        list.add(order4);
        list.add(order5);

        // ================================================ ==========================
        //naturalOrder() represents natural sorting (usually ascending order), and does not handle null. Null pointers may be generated during sorting.
        Comparator<Order> comparator1 = Comparator.comparing(Order::getDateStr, Comparator.naturalOrder());
// List<Order> orderList1 = list.stream().sorted(comparator1).collect(Collectors.toList());
// System.out.println("orderList1:" + orderList1);

        // ================================================ ==========================
        //Comparator.reverseOrder means descending order, null is not processed, and null pointers may be obtained during sorting.
        Comparator<Order> comparator2 = Comparator.comparing(Order::getDateStr, Comparator.reverseOrder());
// List<Order> orderList2 = list.stream().sorted(comparator2).collect(Collectors.toList());
// System.out.println("orderList2:" + orderList2);

        // ================================================ ==========================

        //Comparator.nullsLast means that if the attribute is null, put it at the end
        Comparator<Order> comparator3 = Comparator.comparing(Order::getDateStr, Comparator.nullsLast(naturalOrder()) );
        List<Order> orderList3 = list.stream().sorted(comparator3).collect(Collectors.toList());
// System.out.println("orderList3:" + orderList3);

        // ================================================ ==========================

        //nullsFirst means that if the attribute is null, put it at the front.
        Comparator<Order> comparator4 = Comparator.comparing(Order::getDateStr, Comparator.nullsFirst(Comparator.reverseOrder()));
        List<Order> orderList4 = list.stream().sorted(comparator4).collect(Collectors.toList());
// System.out.println("orderList4:" + orderList4);

        // ================================================ ==========================

        //First sort by the first field in descending order, then by the second field in descending order. If the attribute is null, put it at the end.
        //Output: [Order(price=55.3, dateStr=2021-12-01 00:00:00),
        //Order( price=55.3, dateStr=2021-10-01 00:00:00),
        //Order(price=33.3, dateStr=2021-10-01 00:00:00),
        //Order(price=1.3, dateStr=2021-10-01 00:00:00)]

        Comparator<Order> comparator5 = Comparator.comparing(Order::getDateStr, Comparator.nullsLast(Comparator.reverseOrder()))
                .thenComparing(Order::getPrice, Comparator.nullsLast(Comparator.reverseOrder()));
        List<Order> orderList5 = list.parallelStream().sorted(comparator5).collect(Collectors.toList());
        System.out.println("orderList5:" + orderList5);
    }
}

Stream data
https://www.cnblogs.com/expiator/p/12297003.html