Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Sort HashMap( inside ArrayList) in Android?
    primarykey
    data
    text
    <p>I am having Current Location Latitude &amp; Longitude with me. I am getting the list of ATM's near by the user from the Google places API Web Service and storing my data in Arraylist like below.</p> <p><strong>Code Snippet</strong></p> <pre><code>double currentLat = xxx; double currentLang = xxx; </code></pre> <p><strong>on button click i call the web service of google places.</strong></p> <pre><code>public void buttonClicked(View v) { ParseXMLData xmlData = new ParseXMLData(); url = baseUrl + "location=" + latitude + "," + longitude + "&amp;" + "radius=" + search_Area_Radius + "&amp;" + "name=" + nameofplace + "&amp;" + "sensor=true" + "&amp;" + "key=" + API_KEY; xmlData.execute(""); } </code></pre> <p><strong>Storing the result inside arraylist....</strong></p> <pre><code> for (int i = 0; i &lt; nodes.getLength(); i++) { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); Element e = (Element)nodes.item(i); map.put("Name", XMLFunctions.getValue(e, "name")); map.put("Vicinity", XMLFunctions.getValue(e, "vicinity")); map.put("Latitude", XMLFunctions.getValue(e, "lat")); map.put("Longitude", XMLFunctions.getValue(e, "lng")); loc = new Location(""); loc.setLatitude(Double.parseDouble(XMLFunctions.getValue(e, "lat"))); loc.setLongitude(Double.parseDouble(XMLFunctions.getValue(e, "lng"))); </code></pre> <p><strong>Getting the distance b/w 2 points frome here.</strong></p> <pre><code> distance = currentLoc.distanceTo(loc); map.put("Distance",String.valueOf(distance)); mylist.add(map); loc = null; distance = 0.0; } </code></pre> <p><strong>Implemented the Comparator and Sort the ArrayList</strong></p> <pre><code>public class PositionComparator implements Comparator&lt;HashMap&lt;String, String&gt;&gt; { public PositionComparator() { // TODO Auto-generated constructor stub } public int compare(HashMap&lt;String, String&gt; o1, HashMap&lt;String, String&gt; o2) { /*System.out.println(o1.get("O1 Distance :- ")); System.out.println(o2.get("O2 Distance :- ")); System.out.println(o1.get("Distance").compareTo(o2.get("Distance"))); */ return o1.get("Distance").compareTo(o2.get("Distance")); } } </code></pre> <p><strong>Now what happen is the data which i have store inside the arraylist is sorted but the problem is compareTo method sort data based on the 1st character only. Suppose my data is like 2.12345 and 11.32545 then it will replace as 2-1 = 1.</strong></p> <p><strong>How to solve this?</strong></p> <p>If anyone has any suggestion/tips for this please kindly help me in this.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload