简体   繁体   中英

How to sort List<Map<String,String>> by key1 descending order and key2 ascending order if key1 has same values

I have a List

List<Map<String,String>> list = new ArrayList<>();
            Map<String,String> map = new HashMap<>();
            Map<String,String> map1 = new HashMap<>();
            Map<String,String> map2 = new HashMap<>();
            map.put("productNumber", "107-001");
            map1.put("productNumber", "108-001");
            map2.put("productNumber", "109-001");
            map.put("price", "1.99");
            map1.put("price", "1.02");
            map2.put("price", "1.99");
            list.add(map);
            list.add(map1);
            list.add(map2);

I sort it by price and revers result

formattedResult = list.stream().sorted(Comparator.comparing(m -> Double.parseDouble(m.get("price")))).collect(Collectors.toList());
            Collections.reverse(formattedResult);

The result of this :

**price  productNumber**
1.99   109-001
1.99   107-001
1.02   108-001

I want to sort it like

**price  productNumber**
    1.99   107-001
    1.99   109-001
    1.02   108-001

If prices equal - compare by product number, end first should be productNumber with a lower value. Please help!

You don't need to reverse result if you use the right comparator (and you can chain comparators, too):

    final Comparator<Map<String, String>> byPrice = Comparator.comparing(m -> Double.parseDouble(m.get("price")), Comparator.reverseOrder());

    formattedResult = list.stream().sorted(byPrice.thenComparing(m -> m.get("productNumber"))).collect(Collectors.toList());

It's easier if you build your Comparator in two steps. So try this and print them out. It works like this.

  • first it sorts the price in reversed order.
  • Then t sorts the product number in regular order for equal prices.
    Comparator<Map<String, String>> comp = Comparator
        .comparing(m ->Double.parseDouble(m.get("price"))
                         ,Comparator.reverseOrder());
    comp = comp.thenComparing(m -> m.get("productNumber"));

Apply it like so.

        List<Map<String, String>> formattedResult =
                list.stream().sorted(comp)  
                        .collect(Collectors.toList());


        formattedResult.forEach(m -> System.out.println(
                m.get("price") + " : " + m.get("productNumber")));

This prints

1.99 : 107-001
1.99 : 109-001
1.02 : 108-001


You can create reverse comparator for productNumber

 Comparator<Map<String, String>> productNumberReversed =  Comparator.comparing((Map<String, String> mp) -> mp.get("productNumber")).reversed();

And then add it sort method comparator using thenComparing

formattedResult = list.stream()
           .sorted(Comparator.comparing((Map<String, String> m) -> Double.parseDouble(m.get("price"))).thenComparing(productNumberReversed)).collect(Collectors.toList());
    Collections.reverse(formattedResult);

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