4

I am having the below object

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

How do i sort this based on the value ("Object") in the Map?

0

3 Answers 3

8

Create your own Comparator and use it .

List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
Collections.sort(listMap,new Comparator<Map<String, Object>>() {

            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                //your logic goes here
            }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Use your own Comparator (You have to implement the given interface)

Comments

1

One of the easiest ways to solve using Java 8 is :

Collections.sort(listMap, Comparator.comparing(map -> (String)map.get("key")));

Or,

listMap = listMap.stream().sorted(Comparator.comparing(map -> (String)map.get("key"))).collect(Collectors.toList());

Note: I have converted key(Object) into String. You can use as per your requirements.

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.