1
map = new LinkedHashMap<String, String>(); 
list = new ArrayList<HashMap<String, String>>(); 

map.put("id", id);
map.put("amt", amt);

list.add(map);

How to sort the list with ascending order of amt. I was unable to do this. I'd really appreciate any help.

I am adding id, amt in loop. How to sort with key as amt?

id=1,2,3,4,5
amt=1000,33333,77,9087,5432
harpun
  • 4,022
  • 1
  • 36
  • 40
jason
  • 3,932
  • 11
  • 52
  • 123
  • 1
    Please be more specific about what you exactly want. I can't make head or tail from your problem statement – Prateek Oct 08 '13 at 20:13
  • 2
    Should be same as http://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java Hope it is helpful – hari v Oct 08 '13 at 20:13
  • Hello Prateek.Its a for loop with values --> id and amount as shown above and its added into arraylist -->list .I would like to sort the array in ascending order of amount and get the respective id for the same.Thanks for your time. – jason Oct 08 '13 at 20:17

2 Answers2

3

Simply use TreeMap:

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

map.put("id", "id");
map.put("amd", "amd");

list.add(map);

System.out.println(list);

Output:

[{amd=amd, id=id}]

Now if the Id is in upper case and amd in lower case then you should to override the default behavior of TreeMap using Comparator in the constructor (to ensure that the sorting for strings keys is correct):

Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.toLowerCase().compareTo(o2.toLowerCase());
        }
    });

Look to the TreeMap API Documentation

Husam
  • 2,069
  • 3
  • 19
  • 29
  • Where are you specifying the sort to be done w.r.t to amt and not id ? – jason Oct 08 '13 at 20:21
  • also I have map = new LinkedHashMap(); and list = new ArrayList>(); – jason Oct 08 '13 at 20:23
  • 1
    @jason Strings are sorted lexicographically, since a comes before i the amt comes before id. – arynaq Oct 08 '13 at 20:25
  • How can I make it specific ? if it was not amt but some other alphabet .Trying to be robust in implementation. – jason Oct 08 '13 at 20:27
  • Thanks Magicano that was great explanation.What if I have 4 more variables along with the above id,amt but it still has compare amt.Will the above example still work ? – jason Oct 08 '13 at 21:02
  • Its working for now will get back to you if I have more queries .Thanks for your time. – jason Oct 08 '13 at 21:57
  • I have method to retrieve values from arraylist :: retrieveValuesFromList(list); but I am getting original list not the sorted list the method contains: public static void retrieveValuesFromList(List list) { int size = list.size(); for(int i=0;i o = (Map) list.get(i); String amt; amt=o.get("amt"); Log.e(" amt", amt); String id; id=o.get("id"); Log.e("id", id); } – jason Oct 08 '13 at 22:31
0

Your question is not very clear as at some point you seem to mention sorting a list with only one element (your map). But I assume you want to sort the keys in that map (either inside the map or after adding them all to some list).

However, for sorting lists you have Collections.sort() which can rely on both natural order (if elements implement Comparable) or a custom Comparator. Also, you can have a look at SortedSet (and the TreeSet implementation) if you want to have the order maintained while you add/remove elements.

For maintaining a map with ordered keys, you have the SortMap (with a notable implementation: TreeMap) which will make sure the order it preserved at any moment -- you can add the elements randomly and whenever you iterate you'll get the keys in order. Again the order will can be the natural one (provided by Comparable.compareTo) or you can provide custom ordering by implementing the Comparator interface.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92