3
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  
Cœur
  • 37,241
  • 25
  • 195
  • 267
jason
  • 3,932
  • 11
  • 52
  • 123

5 Answers5

2

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));
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Hello Prabhakaran.Thanks for the code and time but what if the amount has decimal points I mean its not int but rather float/double .How will the changes be made currently I got error in this line return Integer.parseInt(value1)-Integer.parseInt(value2); when I changed it to double then got error in return type. – jason Oct 09 '13 at 06:50
  • Awesome Prabhakaran Thanks for the code and +1.Also I added another string with lots of lat/lng to hashmap how will I get it into arraylist again.ArrayList vertices .Using this to retrieve String idlatlng = map1.get("latlng"); but I get a long list .Please help out Phabhakaran.Thanks. – jason Oct 09 '13 at 07:53
  • @jason Happy to hear from you....Can you please rise another question with your preferred input and expected output....So it would be clear to understand the problem and another thing is many people will give the best answers for your question. Also i will try my best too..... – Prabhakaran Ramaswamy Oct 09 '13 at 08:26
  • Awesome Thanks will do the same now.I will try to do it myself first.Thanks again for your response. – jason Oct 09 '13 at 08:38
  • Hello Sir I just posted the question .Please look into it.Thanks. – jason Oct 09 '13 at 10:35
1

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.

Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48
  • Hello Mr.Mittal but I am getting the error below : Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (List>). The inferred type Map is not a valid substitute for the bounded parameter > – jason Oct 09 '13 at 06:15
1

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!

Community
  • 1
  • 1
Manish Srivastava
  • 1,649
  • 2
  • 15
  • 23
  • Hello Mr.Srivastava I checked your answer but I have and the example is for How should I convert the code.Also if you could edit the code to the one I have shown above it would be of great help.Thanks again for your time really appreciate it. – jason Oct 09 '13 at 06:25
  • 1
    Map map = new TreeMap(); map.put("A", "a"); map.put("B", "b"); System.out.println(map); – Manish Srivastava Oct 09 '13 at 06:27
1

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/

kapil thadani
  • 1,425
  • 14
  • 26
1

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}]
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • Hello JayaMohan .Its working fine till the last comparison(sorting) the last value is not sorted it still stays in same position. – jason Oct 09 '13 at 06:48
  • I had updated the `compare` method. Can you try that. – Jayamohan Oct 09 '13 at 06:56
  • If your amount is having decimal numbers you change change the comparison as `Double(amount1).compareTo(new Double(amount2))`. It will work for both int and decimal values – Jayamohan Oct 09 '13 at 06:59
  • 1
    Hello Jayamohan +1 Awesome !I used Prabhakaran's working code first,I will accept his answer.Thanks again.and Sorry. – jason Oct 09 '13 at 07:49