解决list<Map>中字段排序问题

应用场景:解决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升序排序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值