需求是这样的:
现在有一个list,里面n个map,每个map都有m大小的key-value,且每个map的key的顺序都是一样的,比如说map1的第x个key是y,那么mapn的第x个key也是y。
现在要对list中的每个map进行排序,要按n个map中的相同的key的value值的和
的顺序排序。
举例:比如3个map,都有key1,key2,如果map1.get(key2) + map2.get(key2) + map3.get(key2) > map1.get(key1) + map2.get(key1) + map3.get(key1)
,
那么就要将这三个map中的key的顺序按照key2,key1的顺序排列,原先每个map中key的顺序是key1,key2。
怎么来实现呢
1. First calculate the sorting of the sum of all keys
2. Traverse the original list, according to the order corresponding to the key, and then put it into a list
Ordered
Map
can be achieved withTreeMap
, so I usejava.util.TreeMap
in the example.Generally
SortedMap
can use aComparator
as a construction parameter, then you can write aComparator
yourself to implement sorting. The following is an example