java List Entity Sorting

Posted by john_zakaria on Mon, 11 May 2020 18:36:19 +0200

There are three ways to sort Java entity collections. The first is through a natively supported Comparator, the second is through a Lambda expression in java8, and the third is by using the apache-common toolkit.Tested to achieve the desired results, the third code is more readable and usable, and the third is recommended.

The third sort requires the introduction of Apache-common's beanutils, collections packages, with maven as follows



public class TestCollections {
    private List<Map<String, Object>> getDataList() {
        Map<String, Object> map1 = new HashMap<>(3);
        map1.put("id", 2);
        map1.put("name", "Xiao Ming");

        Map<String, Object> map2 = new HashMap<>(3);
        map2.put("id", 1);
        map2.put("name", "Xiao Zhang");

        Map<String, Object> map3 = new HashMap<>(3);
        map3.put("id", 3);
        map3.put("name", "Little Red");

        List<Map<String, Object>> dataList = new ArrayList<>();
        return dataList;

    public void sort() {
        List<Map<String, Object>> dataList = getDataList();
        System.out.println("Before Sorting:" + dataList);

        // The first Collections sort in descending order
        Collections.sort(dataList, new Comparator<Map>() {
            public int compare(Map o1, Map o2) {
                int id1 = Integer.parseInt(o1.get("id").toString());
                int id2 = Integer.parseInt(o2.get("id").toString());
                //Sort descending by id
                return id2 - id1;
        System.out.println("Collections After descending sort:" + dataList);

        // The second java8 Lambda expression sorts in positive order
        dataList = getDataList();
        dataList.sort((Map o1, Map o2) -> {
            int id1 = Integer.parseInt(o1.get("id").toString());
            int id2 = Integer.parseInt(o2.get("id").toString());
            return id1 - id2;
        System.out.println("Lambda After Positive Sort:" + dataList);

        // Third Kit for Descending Sort
        dataList = getDataList();
        Comparator mycmp1 = ComparableComparator.getInstance();
        //Reverse Order
        mycmp1 = ComparatorUtils.reversedComparator(mycmp1);
        ArrayList<Object> sortFields = new ArrayList<>();
        //Primary Sort (First Sort)
        sortFields.add(new BeanComparator("id", mycmp1));
        // Create a sort chain
        ComparatorChain multiSort = new ComparatorChain(sortFields);
        // Start the real sorting, according to the rules of Primary and Secondary
        Collections.sort(dataList, multiSort);
        System.out.println("After the toolkit is sorted in descending order:" + dataList);

Run Results

Before sorting: [{name=Xiaoming, id=2}, {name=Zhang, id=1}, {name=Xiaohong, id=3}]
After the Collections are sorted in descending order: [{name=Xiaohong, id=3}, {name=Xiaoming, id=2}, {name=Zhang, id=1}]
After Lambda's positive ordering: [{name=tabula, id=1}, {name=Xiaoming, id=2}, {name=Xiaohong, id=3}]
After the toolkit is sorted in descending order: [{name=Xiao Hong, id=3}, {name=Xiao Ming, id=2}, {name=Xiao Zhang, id=1}]

Topics: Lambda Apache Java Maven