Home > Software engineering >  Sort the list of maps by key Value pair in java
Sort the list of maps by key Value pair in java

Time:11-11

I have a JSON Document. I have converted the document as a Map<String, String> in which some of the keys have list of Maps (List<Map<String,String>>) as value. I want to sort this List<Map<String,String>.

Just an example: (after storing the JSON document in Map<String,String>

1. key: name, value: {first=John, last=Doe}
2. key: address, value: null
3. key: birthday, value: 1980-01-01
4. key: company, value: Acme
5. key: occupation, value: Software engineer
6. key: phones, value: [{number=9, type=mobile1}, {number=1, type=mobile}, {type=home, number=0}]
7. key: groups, value: [gym, close-friends]

In the above example in line 6 Key = "phones" has value as list of maps that I have to sort.

Expected Output:

1. key: name, value: {first=John, last=Doe}
2. key: address, value: null
3. key: birthday, value: 1980-01-01
4. key: company, value: Acme
5. key: occupation, value: Software engineer
6. key: phones, value: [{number=0, type=home}, {number=1, type=mobile}, {number=9, type=mobile1}]
7. key: groups, value: [gym, close-friends]

CodePudding user response:

I want to sort this List<Map<String,String>.

Given the data you show all the maps have exactly one entry. So we sort by the keys of this entry. Specifically by their integer values.

List<Map<String, String>> myMapList 
      = // get list of maps here
    Collections.sort(myMapList, new Comparator<Map<String, String>>() {
        @Override
        public int compare(
          Map<String, String> m1, Entry<String, String> m2) {
            var key1 = m1.keySet().iterator().next();
            var key2 = m2.keySet().iterator().next()
            return Integer.valueOf(key1).compareTo(Integer.valueOf(key2));
        }
    });

For more details check this out.

Does this solve your problem ? Let me know in the comments.

CodePudding user response:

You could use List.sort():

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Main {
    private static final String TYPE = "type";
    private static final String NUMBER = "number";

    public static void main(String[] args) {
        Map<String, List<Map<String, String>>> jsonMap = new HashMap<>();
        jsonMap.put("phones", new ArrayList<>(Arrays.asList(
                                    Map.of(NUMBER, "9", TYPE, "mobile1"), 
                                    Map.of(NUMBER, "1", TYPE, "mobile"),
                                    Map.of(TYPE, "home", NUMBER, "0"))));
        
        System.out.println("Before:");
        System.out.println(jsonMap);

        jsonMap.get("phones").sort((a, b) -> 
            Integer.valueOf(a.get(NUMBER)).compareTo(Integer.valueOf(b.get(NUMBER))));

        System.out.println("After:");
        System.out.println(jsonMap);
    }

}

Output:

Before:
{phones=[{type=mobile1, number=9}, {type=mobile, number=1}, {type=home, number=0}]}
After:
{phones=[{type=home, number=0}, {type=mobile, number=1}, {type=mobile1, number=9}]}
  • Related