3
votes
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  
5
check my answer and given link you will got your answer only there. you can't sort map by value without any custom method.Manish Srivastava
Hello Manish How do I apply the changes to existing code above.could you please edit it with changes.It would be of great help.Thanksjason
Sory dear! everything infront of you just try it yourself. no one going to cook food for you.Manish Srivastava

5 Answers

2
votes

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

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.

1
votes

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!

1
votes

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/

1
votes

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}]