I have a TreeMap that holds the following keys/values:
private ArrayList<TreeMap<String,String>> mList = new ArrayList<TreeMap<String,String>>();
TreeMap<String,String> item = new TreeMap<String,String>();
item.put("FirstName", strFirstName);
item.put("LastName", strLastName);
item.put("Country", strCountry);
mList.add( item );
How can I sort the TreeMap by the values stored under the “LastName” key after adding all the items? I would like to have a TreeMap in the same format as the original list but with the mentioned sorting applied:
“James”, “Amish”, “USA”
“Charles”, “Brentwood”, “USA”
“Jake”, “Cornell”, “USA”
“Amy”, “Dunn”, “USA”
“Melinda”, “Ellis”, “Canada”
I’ve found a similar question relating to sorting a TreeMap by values in ‘regular’ Java, but the code didn’t work for me – I don’t know if because of Android pecularities or my inability to adapt it to fit my needs :-/
Thanks,
Nick
It sounds like you are trying to sort the ArrayList containing the TreeMaps, correct?
If so, the following should suit your needs:
It should be noted that the use of a TreeMap to contain a list of name/value pairs is probably not necessary – I would just create a class to hold the values.