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;
}