简体   繁体   中英

Sorting a list of Map<String, String> based on a particular numeric value

I have a List<Map<String, String>> . One particular value of the map is a numeric entry with decimals. I wish to sort the list in descending order based on that particular value of the map.

for example:

List<Map<String, String>> foo= new ArrayList<Map<String, String>>();
Map<String, String> bar1 = new HashMap<>();
Map<String, String> bar2 = new HashMap<>();
Map<String, String> bar3 = new HashMap<>();

bar1.put("name", "abc");
bar1.put("score", "72.5");
bar1.put("sex", "male"); 
foo.add(bar1);

bar2.put("name", "pqr");
bar2.put("score", "98.7");
bar2.put("sex", "female"); 
foo.add(bar2);

bar3.put("name", "xyz");
bar3.put("score", "100.0");
bar3.put("sex", "male"); 
foo.add(bar3);
.
.
.
.

and so on

I want to sort the List<Map<String, String>> in descending order such that the map containing the score of 100.0 is on top.

I tried

Collections.sort( list, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return (o1.getValue()).compareTo(o2.getValue());
    }
});

but the "V" here is a string, while i need it to be sorted as a float. Any help would be appreciated.

If is not possible to create a class from that map then you can do something like:

Collections.sort(foo, (o1, o2) -> {
        return new BigDecimal(o2.get("score")).compareTo(new BigDecimal(o1.get("score")));
    });

or if you are not using java 8:

Collections.sort(foo, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                return new BigDecimal(o2.get("score")).compareTo(new BigDecimal(o1.get("score")));
            }
        });

Why you need List Of map , you can go with List<SomeClass> and sort it as you wish, where Some class will hold you value for name sex score.

Edit: You can use convert your String in float or can take float as Someclass data type, it make your code very easy.

This could be a another simple solution.

Use Float to parse the string and use existing compare method of Float.

Collections.sort(foo, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
               return Float.compare(Float.parseFloat(o2.get("score")), Float.parseFloat(o1.get("score")));
            }
        });

to sort a list of map by values of specific key :

public static void sortMapByKey(List<Map<String, Object>> crList, final String sortKey, final boolean ascending) {
            Collections.sort(crList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Object obj1 = o1.get(sortKey);
                    Object obj2 = o2.get(sortKey);
                    if (obj1 != null && obj2 != null) {
                        if (ascending)
                            return obj1.toString().compareTo(obj2.toString());
                        else
                            return obj2.toString().compareTo(obj1.toString());
                    } else
                        return 0;
                }
            });
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM