List<Map<String ,String>> 数据比较后排序
方法一:
测试代码:
List<Map<String,String>> bufferList = new ArrayList<Map<String,String>>();
List<Map<String,String>> conversationList = new ArrayList<Map<String,String>>();
Map<String,String> m1 = new HashMap<>();
m1.put("counter","4.5");
Map<String,String> m2 = new HashMap<>();
m2.put("counter","8.5");
Map<String,String> m3 = new HashMap<>();
m3.put("counter","9.5");
Map<String,String> m4 = new HashMap<>();
m4.put("counter","0.5");
Map<String,String> m5 = new HashMap<>();
m5.put("counter","9.5");
Map<String,String> m6 = new HashMap<>();
m6.put("counter","11.5");
Map<String,String> m7 = new HashMap<>();
m7.put("counter","10.5");
bufferList.add(m1);
bufferList.add(m2);
bufferList.add(m3);
bufferList.add(m4);
bufferList.add(m5);
bufferList.add(m6);
bufferList.add(m7);
Map<String,String> m11 = new HashMap<>();
m11.put("sga","4.5");
Map<String,String> m21 = new HashMap<>();
m21.put("sga","8.5");
Map<String,String> m31 = new HashMap<>();
m31.put("sga","9.5");
Map<String,String> m41 = new HashMap<>();
m41.put("sga","0.5");
Map<String,String> m51 = new HashMap<>();
m51.put("sga","11.5");
Map<String,String> m61 = new HashMap<>();
m61.put("sga","22.5");
Map<String,String> m71 = new HashMap<>();
m71.put("sga","22.5");
conversationList.add(m11);
conversationList.add(m21);
conversationList.add(m31);
conversationList.add(m41);
conversationList.add(m51);
conversationList.add(m61);
conversationList.add(m71);
//排序
Collections.sort(bufferList, new Comparator<Map<String, String>>() {
public int compare(Map<String, String> o1, Map<String, String> o2) {
String name1 = o1.get("counter") ;
String name2 = o2.get("counter") ;
//return name1.compareTo(name2);
// 修改之后 增加类型转换 返回比较数据数据
Double doubleName1 = Double.parseDouble(name1);
Double doubleName2 = Double.parseDouble(name2);
return doubleName2.compareTo(doubleName1);
}
});
//排序
Collections.sort(conversationList, new Comparator<Map<String, String>>() {
public int compare(Map<String, String> o1, Map<String, String> o2) {
String name1 = o1.get("sga") ;
String name2 = o2.get("sga") ;
//return name2.compareTo(name1);
// 修改之后 增加类型转换 返回比较数据数据
Double doubleName1 = Double.parseDouble(name1);
Double doubleName2 = Double.parseDouble(name2);
return doubleName2.compareTo(doubleName1);
}
});
int bufferCount = bufferList.size();
int converCount = conversationList.size();
map.put("bufferList", bufferList.size()<=5 ? bufferList.subList(0, bufferCount) : bufferList);
map.put("conversationList", conversationList.size()<=5 ? conversationList.subList(0, converCount) : conversationList);