0

I'm trying to sort a List of Map object which looks something like this.

"list" : [ 
        {
            "up" : false,
            more fields ....
        }, 
        {
            "randomField" : "ABC",
            "someMoreRandomField" : "Retirement"
            more fields ....
        }, 
        {
            "up" : false,
            more fields ....
        },
        {
            "last" : true,
            "up" : false
            more fields ....
        }, 
        {
            "last" : true,
            "up" : true
            more fields ....
        },      
        {
            "up" : true
            more fields ....
        },
        {
            "up" : true
            more fields ....
        }, 
        {
            "up" : true
            more fields ....
        }

    ]

I want them to be sorted in based on these conditions. If Object has the key 'last' marked as true then they should be last in the list. If the key 'up' is true, they will appear first in the list. If the key 'last' marked as true will take precedence and should appear bottom of the list even if the same object has 'up' marked as true.

Sorted list:

"list" : [ 
        {
            "up" : true
            more fields ....
        },
        {
            "up" : true
            more fields ....
        }, 
        {
            "up" : true
            more fields ....
        },
        {
            "up" : false,
            more fields ....
        }, 
        {
            "randomField" : "ABC",
            "someMoreRandomField" : "Retirement"
            more fields ....
        }, 
        {
            "up" : false,
            more fields ....
        },
        {
            "last" : true,
            "up" : false
            more fields ....
        }, 
        {
            "last" : true,
            "up" : true
            more fields ....
        }
    ]

I was able to achieve this using Vanilla JS, trying to the do the same in Java 8. Have been at it for quite some time, stumbled upon few answers: https://stackoverflow.com/a/46717991/2114024

But haven't found the solution yet.

JS code snippet:

list.sort(function(a,b)  {
     if(a.last === undefined) {a.last = false};
     if(b.last === undefined) {b.last = false};
     if(a.up === undefined) {a.up = false};
     if(b.up === undefined) {b.up = false};
     return Number(a.last) - Number(b.last) || Number(b.up) - Number(a.up);
});

Any suggestions are appreciated.

2 Answers 2

1

I have tried the similar logic that you applied for java script. You can try with the following code in java 8:

public class ListMapSort {

    public static void main(String[] args) {
        List<Map<String, Object>> data = getData();

        data.sort(Comparator.comparing(m -> {
            int lastKey = (m.containsKey("last") && (boolean)m.get("last")) ? 1 : 0;
            int upKey = (m.containsKey("up") && (boolean)m.get("up")) ? 0 : 1;
            return lastKey*10 + upKey;
        }));

        System.out.println("Sorted data: " + data);
    }

    private static List<Map<String, Object>> getData() {
        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("up", false);
        map.put("id", 1);
        data.add(map);
        map = new HashMap<>();
        map.put("up", false);
        map.put("id", 2);
        data.add(map);
        map = new HashMap<>();
        map.put("last", false);
        map.put("up", false);
        map.put("id", 3);
        data.add(map);
        map = new HashMap<>();
        map.put("last", true);
        map.put("up", true);
        map.put("id", 4);
        data.add(map);
        map = new HashMap<>();
        map.put("up", true);
        map.put("id", 5);
        data.add(map);
        map = new HashMap<>();
        map.put("last", false);
        map.put("id", 6);
        data.add(map);
        map = new HashMap<>();
        map.put("last", true);
        map.put("id", 7);
        data.add(map);
        map = new HashMap<>();
        map.put("last", false);
        map.put("up", true);
        map.put("id", 7);
        data.add(map);

        return data;
    }
}

As it has been said in the other post mentioned by you, it can be solved using this way as well,

data.sort(Comparator.comparing(
                (Map<String, Object> m) -> (boolean)m.getOrDefault("last", false))
                .thenComparing(m -> !(boolean)m.getOrDefault("up", false)));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your input. I had to reverse the order using Comparator, but I am gonna accept your answer. :)
1

I was able to sort it using @swapan's answer, but had to reverse the negations.

data.sort(Comparator.comparing(
            (Map<String, Object> m) -> !(boolean)m.getOrDefault("last", false))
            .thenComparing(m -> (boolean)m.getOrDefault("up", false)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.