应用场景:解决list
class Demo02{
public static void main(String[] args) {
Map<String, String> map1 = new HashMap<>();
Map<String, String> map2 = new HashMap<>();
map1.put("name", "zhangsan");
map1.put("strtype", "全资");
map1.put("subconam", "1000");
map1.put("condate", "2020-05-06");
map1.put("fundedratio", "50.66%");
map2.put("name", "lisi");
map2.put("strtype", "全资");
map2.put("subconam", "2000");
map2.put("condate", "2020-06-06");
map2.put("fundedratio", "49.55%");
ArrayList<Map<String, String>> list = new ArrayList<>();
list.add(map1);
list.add(map2);
Collections.sort(list,new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
String f1 = o1.get("fundedratio").replaceAll("%", "");
String f2 = o2.get("fundedratio").replaceAll("%", "");
double d1 = Double.parseDouble(f1);
double d2 = Double.parseDouble(f2);
double v = d2-d1;
int i =0;
if (v>=0) {
i=1;
}else {
i=-1;
}
System.out.println(i);
return i;
}
});
System.out.println("v:"+v);
System.out.println("d1:"+d1+"-----" + "d2:"+d2);
System.out.println("f1:"+f1);
System.out.println(list.get(0));
list.get(0).get("name");
}
output:
f1:49.55
d1:49.55-----d2:50.66
v:1.1099999999999994
1
{strtype=全资, name=zhangsan, subconam=1000, fundedratio=50.66%, condate=2020-05-06}
注意:
o2-o1倒序排序
o1-o2升序排序