0

I have a hash map list containing contact name as key and phone number as value, like this.

   cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {

        name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        //stored_Contacts.add(name);
       // phone_number_list.add(phonenumber);


        try {
            hash_map_list = new ArrayList<HashMap<String, String>>();
            map = new HashMap<String, String>();
            map.put(name, phonenumber);
            hash_map_list.add(map);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    cursor.close();

What i need is ,i want to sort the contact names in to alphabetical order and keep the values(phone numbers),with contact names.Can anyone help ?

I have tried this function, not working.

 private static Map<String, String> sortByComparator(Map<String, String> unsortMap, final boolean order)
{

    List<Map.Entry<String, String>> list = new LinkedList<Map.Entry<String, String>>(unsortMap.entrySet());

    // Sorting the list based on values
    Collections.sort(list, new Comparator<Map.Entry<String, String>>()
    {
        public int compare(Map.Entry<String, String> o1,
                           Map.Entry<String, String> o2)
        {
            if (order)
            {
                return o1.getValue().compareTo(o2.getValue());
            }
            else
            {
                return o2.getValue().compareTo(o1.getValue());

            }
        }
    });

    Map<String, String> sortedMap = new LinkedHashMap<String, String>();
    for (Map.Entry<String, String> entry : list)
    {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}
Jack
  • 1,825
  • 3
  • 26
  • 43
  • 2
    Use `TreeMap` instead of `HashMap`. Done. – Andreas May 10 '17 at 05:56
  • 1
    why not to use arraylist ? – Pavan May 10 '17 at 05:58
  • @pavan, how can I do this with ArrayList? – Jack May 10 '17 at 06:03
  • check this one it can help http://stackoverflow.com/questions/7316754/how-to-display-contacts-in-alphabetic-order-in-listview – Pavan May 10 '17 at 06:05
  • what is your requirement if you are displaying it in list or else, if in list you can make an arraylist with one object which contain all values like name, number also if you want to add any extra properties you dont need to change your code you can just add one new property. – Pavan May 10 '17 at 06:07

1 Answers1

3

A HashMap is not designed to be a sorted data set. This was sacrificed in order to make it as efficient as possible when storing and retrieving values.

A TreeMap, on the other hand, is designed in such a way that your keys are always sorted. This looks to be the appropriate tool for your use case.

Joe C
  • 15,324
  • 8
  • 38
  • 50