Android: How to sort data for ListView?
<p>I have some JSON data I am pulling down from a server. One of the fields in this data is a distance value. I need to have the data sorted by distance from lowest to highest in the ListView. I am not sure how to go about doing this?</p>

<p>Any help appreciated. </p>

<p>This is my code to grab the data not sure how to get it sorted properly?</p>

<pre><code>// Hashmap for ListView
ArrayList&lt;HashMap&lt;String, String&gt;&gt; bathroomList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.activity_search);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        bathrooms = json.getJSONArray(TAG_BATHROOMS);

        // looping through All Contacts
        for(int i = 0; i &lt; bathrooms.length(); i++){
            JSONObject c = bathrooms.getJSONObject(i);


            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String access = c.getString(TAG_ACCESS);
            String city = c.getString(TAG_CITY);
            String comment = c.getString(TAG_COMMENT);
            String directions = c.getString(TAG_DIRECTIONS);
            String name = c.getString(TAG_NAME);
            String street = c.getString(TAG_STREET);
            String bathroomtype = c.getString(TAG_BATHROOMTYPE);                
            String distance = c.getString(TAG_DISTANCE);
            String distanceTrimmed = distance.substring(0,4) + " " + "miles away";
            String avail = c.getString(TAG_AVAIL);
            String country = c.getString(TAG_COUNTRY);
            String state = c.getString(TAG_STATE);
            String postal = c.getString(TAG_POSTAL);

            //System.out.println(name);

            // creating new HashMap
            HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();

            // adding each child node to HashMap key =&gt; value
            map.put(TAG_ID, id);
            map.put(TAG_ACCESS, access);
            map.put(TAG_CITY, city);
            map.put(TAG_COMMENT, comment);
            map.put(TAG_DIRECTIONS, directions);
            map.put(TAG_NAME, name);
            map.put(TAG_STREET, street);
            map.put(TAG_BATHROOMTYPE, bathroomtype);
            map.put(TAG_DISTANCE, distanceTrimmed);
            map.put(TAG_AVAIL, avail);
            map.put(TAG_COUNTRY, country);
            map.put(TAG_STATE, state);
            map.put(TAG_POSTAL, postal);

            // adding HashList to ArrayList
            bathroomList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    /**
     * Updating parsed JSON data into ListView
     * */

    ListAdapter adapter = new SimpleAdapter(this, bathroomList,
            R.layout.list_item,
            new String[] { TAG_ID, TAG_NAME, TAG_STREET, TAG_CITY, TAG_STATE, TAG_POSTAL,TAG_COUNTRY, TAG_DISTANCE, TAG_DIRECTIONS, TAG_COMMENT, TAG_AVAIL, TAG_BATHROOMTYPE, TAG_ACCESS}, new int[] {
                     R.id.key,R.id.name, R.id.street, R.id.city, R.id.state, R.id.postal,R.id.country, R.id.distance, R.id.directions, R.id.comments, R.id.availability, R.id.bathroomtype, R.id.access });

    setListAdapter(adapter);
</code></pre>
