-1

I am fetching JSON Data from server and able to parse JSON Data by code. now storing it in hashmap and then adding to arraylist and displaying in listview everything seems working fine.

  HashMap<String, String> map = new HashMap<String, String>();
                                         map.put(Constants.KEY_REFERENCE,p.reference);                      map.put(Constants.KEY_NAME, p.name);
map.put("distance","p.distance");
ArrayListItems.add(map);

Now,When i display the data in listview it works fine ,but now i want to sort the distance in ascending order ,with least starting from top . Any help will be appreciated .Thanks in advance

now i am able to sort but there is something wrong with the distance when i display it shows 1.23, 1.45, 2.39, 3.56, and then it shows 0.38, 0.56.

Its not sorting for values starting with 0

So please help me.

Nikhil Virupakshi
  • 482
  • 2
  • 13
  • 26
  • 1
    possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) with custom object == `HashMap` and "property" == `HashMap.get("propertyName")` ... – Selvin Sep 16 '13 at 11:22
  • 1
    use SortedMap instead of Map. http://stackoverflow.com/questions/7427758/java-how-to-use-sortedmap and http://www.roseindia.net/java/example/java/util/sortedmap.shtml – Pankaj Kumar Sep 16 '13 at 11:29

1 Answers1

2

I would look at Collections.sort(); and how you can use the Comparable interface to sort the HashMap (by overriding the compare() method). The following article is quite useful, because if I am not mistaken, there are two distinct ways in which you can sort a HashMap; one by it's keys and the other by it's values.

blackpanther
  • 10,998
  • 11
  • 48
  • 78