Unable to sort the list by ascending order

岁酱吖の 提交于 2019-12-10 10:21:06

问题


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  

回答1:


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));



回答2:


Use sort()

Ex:

list.add(map);
Collections.sort(list)

System.out.println(list)

it will now print the list in ascending order of the type of content it contains.




回答3:


No.. You can't short any map according value using default sort method. You should write your custom method for sorting. see this link- TreeMap sort by value

and I suggest use treemap if you want sorted by key..

Thanks!




回答4:


You have to use custom Comparator and pass it in Treemap's constructor. For example to sort it with amount use following comparator :

Refer the following link. It will definitely solve your problem

http://java2novice.com/java-collections-and-util/treemap/comparator-user-object/




回答5:


By default the list is not sorted. You need Collections.sort() method that takes Comparator. So looks like you want to sort by amount, you should implement the comparator like below.

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        String amount1 = o1.get("amount");
        String amount2 = o2.get("amount");
        return new Integer(amount1).compareTo(new Integer(amount2));
    }
});

Here is the full working copy,

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

Map<String, String> map1 = new TreeMap<String, String>();
map1.put("id", "2");
map1.put("amount", "200");

Map<String, String> map2 = new TreeMap<String, String>();
map2.put("id", "1");
map2.put("amount", "100");

Map<String, String> map3 = new TreeMap<String, String>();
map3.put("id", "3");
map3.put("amount", "300");

list.add(map1);
list.add(map2);
list.add(map3);

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        String amount1 = o1.get("amount");
        String amount2 = o2.get("amount");
        return amount1.compareTo(amount2);
    }
});

System.out.println(list);

It should print,

[{amount=100, id=1}, {amount=200, id=2}, {amount=300, id=3}]


来源:https://stackoverflow.com/questions/19264271/unable-to-sort-the-list-by-ascending-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!