简体   繁体   English

对地图列表进行排序 <String, String> 基于特定的数值

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

I have a List<Map<String, String>> . 我有一个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. 我想按降序对List<Map<String, String>>进行排序,以使包含100.0分的地图位于顶部。

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. 但是这里的“ V”是一个字符串,而我需要将其作为浮点数进行排序。 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: 或者如果您没有使用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. 为什么需要List Of map ,您可以使用List<SomeClass>并根据需要对其进行排序,其中Some类将为您保留name sex score.价值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. 编辑:您可以使用float转换String或将float用作Someclass数据类型,这使您的代码非常容易。

This could be a another simple solution. 这可能是另一个简单的解决方案。

Use Float to parse the string and use existing compare method of Float. 使用Float解析字符串,并使用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;
                }
            });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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