Home > Back-end >  Ordered List <Map> more keywords, to a great spirit
Ordered List <Map> more keywords, to a great spirit

Time:12-01

The initial List
[{aaa=10, BBB=2, CCC=6},
{aaa=6, BBB=2, CCC=1},
{aaa=8, BBB=3, CCC=5},
{aaa=10, BBB=4, CCC=4},
{aaa=10, BBB=2, CCC=3}]


Now demand is first according to the size of the aaa, from big to small order, if the values are the same, aaa is sorted by BBB from big to small, if BBB values are the same, are sorted by CCC from big to small
For example, sorted into a
[{aaa=10, BBB=4, CCC=4},
{aaa=10, BBB=2, CCC=6},
{aaa=10, BBB=2, CCC=3}
{aaa=8, BBB=3, CCC=5},
{aaa=6, BBB=2, CCC=1},]

to a great god!

CodePudding user response:

This is a dead code
 
List. The sort (new Comparator<linkedHashMap () {

@ Override
Public int the compare (LinkedHashMap O1, LinkedHashMap O2) {
If (o2) get (" aaa ") - o1. Get (" aaa ") & gt; 0 {
return 1;
} else if (o2) get (" aaa ") - o1. Get (" aaa ") & lt; 0 {
return -1;
} else {
If (o2) get (" BBB ") - o1. Get (" BBB ") & gt; 0 {
return 1;
} else if (o2. Get. (" BBB ") - o1 get (" BBB ") & lt; 0 {
return -1;
} else {
If (o2) get (" CCC ") - o1. Get (" CCC ") & gt; 0 {
return 1;
} else if (o2) get (" CCC ") - o1. Get (" CCC ") & lt; 0 {
return -1;
} else {
return 0;
}
}
}
}
});

CodePudding user response:

 String json="[{aaa: 10, BBB: 2, CCC, 6}, \ n +" 
"{aaa: 6, BBB: 2, CCC: 1}, \ n" +
"{aaa: 8, BBB: 3, CCC, 5}, \ n" +
"{aaa: 10, BBB: 4, CCC, 4}, \ n" +
"{aaa: 10, BBB: 2, CCC: 3}]".
List List=JSON. ParseArray (JSON, HashMap class);
List Collect=list. Stream ()
Sorted (Comparator
Comparing (o - & gt; Integer. The valueOf (String. The valueOf (((Map) o). Get (" aaa "))), the Comparator. ReverseOrder ())
ThenComparing (o - & gt; Integer. The valueOf (String. The valueOf (((Map) o). Get (" BBB "))), the Comparator. ReverseOrder ())
ThenComparing (o - & gt; Integer. The valueOf (String. The valueOf (((Map) o). Get (" CCC "))), the Comparator. ReverseOrder ())
)
Collect (Collectors. ToList ());


Comparator
Comparing ()
ThenComparing ()
ThenComparing ()

CodePudding user response:

Rewrite the Comparator class
  • Related