无法按升序排序顺序列表 [英] Unable to sort the the list by ascending order

查看:114
本文介绍了无法按升序排序顺序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 地图<字符串,字符串>图;
清单<地图<字符串,字符串>>名单=新的ArrayList<地图<字符串,字符串>>();
/////的OnCreate .............
功能1(){
地图=新TreeMap的<字符串,字符串>();
map.put(ID,ID);
map.put(入账金额,金额);list.add(地图);的System.out.println(名单);
}

id的输入值= 1,3,5,57,80

有关量的输入值= 100,500,200,10,10000

无法按升序排序多少的顺序列表。它仍然显示了它被插入的顺序。

我该如何解决这个问题?我AP preciate任何帮助。先谢谢了。

预计产量:量的升序:

  AMT = 10 ID = 4
AMT = 100的id = 1
AMT = 200的id = 3
AMT = 500 ID = 2
AMT = 10000 ID = 5


解决方案

假设这是你输入

 地图<字符串,字符串>图;
  清单<地图<字符串,字符串>>名单=新的ArrayList<地图<字符串,字符串>>();
  地图=新TreeMap的<字符串,字符串>();
  map.put(ID,1);
  map.put(量,100);
  list.add(地图);
  地图=新TreeMap的<字符串,字符串>();
  map.put(ID,2);
  map.put(量,500);
  list.add(地图);
  地图=新TreeMap的<字符串,字符串>();
  map.put(ID,3);
  map.put(量,200);
  list.add(地图);
  地图=新TreeMap的<字符串,字符串>();
  map.put(ID,4);
  map.put(量,10);
  list.add(地图);
  地图=新TreeMap的<字符串,字符串>();
  map.put(ID,5);
  map.put(额,10000);
  list.add(地图);

下面是您的排序code

  Col​​lections.sort(名单,新的比较<地图<字符串,字符串>>(){        @覆盖
        公众诠释比较(地图<字符串,字符串> 01,地图<字符串,字符串> O2){
            字符串值1 = o1.get(额);
            字符串值2 = o2.get(额);
            返回的Integer.parseInt(值)-Integer.parseInt(值2);
        }
    });    对于(地图<字符串,字符串> MAP1:名单){
        字符串ID = map1.get(ID);
        串金额= map1.get(额);
        的System.out.println(金额=量+ +,+的id =+ ID);
    }

输出

 额= 10,ID = 4
量= 100,ID = 1
量= 200,ID = 3
量= 500,ID = 2
量= 10000,ID = 5

更新

替换返回的Integer.parseInt(值)-Integer.parseInt(值2); 通过以下code。如果值是小数

 返回Double.valueOf(值).compareTo(Double.valueOf(值2));

Map<String, String> map ;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();


/////OnCreate.............


function1(){
map = new TreeMap<String, String>();
map.put("id", "id");
map.put("amont", "amount");

list.add(map);

System.out.println(list);
}

input values for id=1,3,5,57,80

input values for amount=100,500,200,10,10000

Unable to sort the list by ascending order of amount. It still shows in the order it was inserted.

How do I fix this? I appreciate any help. Thanks in advance.

Expected output: Ascending order of amount:

amt=10 id=4  
amt=100 id=1  
amt=200 id=3  
amt=500 id=2  
amt=10000 id=5  

解决方案

Assuming this is your input

  Map<String, String> map ;
  List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  map = new TreeMap<String, String>();
  map.put("id","1");
  map.put("amount","100");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","2");
  map.put("amount","500");  
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","3");
  map.put("amount","200");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","4");
  map.put("amount","10");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","5");
  map.put("amount","10000");
  list.add(map);

Here is your sorting code

  Collections.sort(list, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            String value1 =  o1.get("amount");
            String value2 =  o2.get("amount");
            return Integer.parseInt(value1)-Integer.parseInt(value2);
        }
    });

    for (Map<String, String> map1 : list) {
        String id = map1.get("id");
        String amount = map1.get("amount");
        System.out.println("amount= "+amount + " , " +"id = "+id);
    }

Output

amount= 10 , id = 4
amount= 100 , id = 1
amount= 200 , id = 3
amount= 500 , id = 2
amount= 10000 , id = 5

update

Replace return Integer.parseInt(value1)-Integer.parseInt(value2); with the following code if the values are decimal.

return Double.valueOf(value1).compareTo(Double.valueOf(value2));

这篇关于无法按升序排序顺序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆