简体   繁体   中英

Sort List<Map<String,Object>> based on value

Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map.

The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it:

So far

List<Map<String,Object>> data = getResults();
Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1")))
              .compareTo((String.valueOf(o1.get("Field1")))));

It is not a great Idea since numbers are not properly sorted.... How can I handle this?

You can generalize comparing numbers by comparing their double values , because they are the largest. If two objects cannot be cast to numbers and the double value cannot be parsed from these objects, then compare their string values :

List<Map<String, Object>> data = Arrays.asList(
        Map.of("Field1", 21.2d),  // Double
        Map.of("Field1", "qqq"),  // String
        Map.of("Field1", "22.5"), // String
        Map.of("Field1", 2),      // Integer
        Map.of("Field1", 3L),     // Long
        Map.of("Field1", 23.1f)); // Float
data.sort(Comparator.comparingDouble((Map<String, Object> map) -> {
    Object object = map.get("Field1");
    if (object instanceof Number) {
        return ((Number) object).doubleValue();
    } else {
        try {
            return Double.parseDouble(String.valueOf(object));
        } catch (NumberFormatException e) {
            return Double.NaN;
        }
    }
}).thenComparing(map -> String.valueOf(map.get("Field1"))));
data.forEach(System.out::println);
// {Field1=2}
// {Field1=3}
// {Field1=21.2}
// {Field1=22.5}
// {Field1=23.1}
// {Field1=qqq}

See also: Sort 2D List by Column Header

I think the value in the map should all be instances of Comparable, so something like this might work. This solution can be better than the accepted answer since you can put your own class as values(such as MyClass which implements Comparable)

List<Map<String, Object>> data = getResults();

Collections.sort(data, (o1, o2) -> {
    Object v1 = o1.get("Field1");
    Object v2 = o2.get("Field1");
    if (v1 instanceof Comparable) {
        // may throw exception if v1 and v2 are not
        // the same type, eg Double compare String
        return ((Comparable) v1).compareTo((Comparable) v2);
    } else {
        return String.valueOf(v1).compareTo(String.valueOf(v2));
    }
});

Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map.

The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it:

So far

List<Map<String,Object>> data = getResults();
Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1")))
              .compareTo((String.valueOf(o1.get("Field1")))));

It is not a great Idea since numbers are not properly sorted.... How can I handle this?

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