2

I want to sort an Hashmap by the object's value. In this case, by country code.

  KEY              OBJECT                        
  String        LoyaltyCountry
                     - country name
                     - country code
                     - country loc

My code is as follows:

public static HashMap<String, LoyaltyCountry> loyaltyCountrySortMap(HashMap<String, LoyaltyCountry> loyaltyCountryMap) {

            if (loyaltyCountryMap != null) {
                List keys = new ArrayList();
                keys.addAll(loyaltyCountryMap.keySet());
                Collections.sort(keys, new Comparator<LoyaltyCountry>() {
                    public int compare(LoyaltyCountry o1, LoyaltyCountry o2) {
                        return o1.getCountryName().compareTo(o2.getCountryName());
                    }
                });
            }

            return loyaltyCountryMap;
        }

How can I do this correctly?

newbie
  • 14,582
  • 31
  • 104
  • 146
  • You're trying to sort the HashMap itself? Or are you trying sort the keys? Or values? Please clarify. Your code seems to imply you want to sort the keys. – lrAndroid Apr 18 '12 at 16:53
  • the hashmap... by the value (country name) – newbie Apr 18 '12 at 16:54
  • 2
    possible duplicate of http://stackoverflow.com/q/10167278/1332690 – Charles Menguy Apr 18 '12 at 16:55
  • What, exactly, are you trying to sort in it? HashMaps are structured to provide fast put/get, and changing the sorting methods in the HashMap defeats the purpose of using a map in the first place. – lrAndroid Apr 18 '12 at 16:55
  • I am trying to sort the Country Name... And by sorting it, the whole hashmap will be sorted accordingly... – newbie Apr 18 '12 at 16:57
  • 1
    I think you're filling a map with a list of sorted/unsorted values and then you want to sort it by value. I'll recommend to get the list already sorted and then use a `LinkedHashMap` – Luiggi Mendoza Apr 18 '12 at 17:01

3 Answers3

7

Here is a method that returns a Set containing the Map.Entry items sorted by their value.

public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> entriesSortedByValues(Map<K, V> map) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(
            new Comparator<Map.Entry<K, V>>() {
                @Override
                public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
                    return e1.getValue().compareTo(e2.getValue());
                }
            });
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}
Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
1

Create a Comparator<Map.Entry<String,LoyaltyCountry>>, get the Map.entrySet(), put it into a List<Map.Entry<String,LoyaltyCountry>> and sort it.

Clarification: You say you want to sort the HashMap by values -- that isn't going to happen. A HashMap is an unsorted implementation of a Map. Moreover, the sorted implementations of Map all sort on keys, so even a TreeMap won't help you.

The closest you can come to it (short of making your own map implementation) is produce a list of the map entries, which is what I described how to do above.

trutheality
  • 23,114
  • 6
  • 54
  • 68
1

You can't sort HashMap.

Looks like you want to return an ordered list of map's values, sorted by country name.

For that change return type of your method to List, e.g

public static List<LoyaltyCountry> loyaltyCountrySortMap(
    HashMap<String, LoyaltyCountry> loyaltyCountryMap
  ) 
{

    if (loyaltyCountryMap != null) {
        List<LoyaltyCountry> values = new ArrayList();
        values.addAll(loyaltyCountryMap.values());
        Collections.sort(values, new Comparator<LoyaltyCountry>() {
            public int compare(LoyaltyCountry o1, LoyaltyCountry o2) {
                return o1.getCountryName().compareTo(o2.getCountryName());
            }
        });

        return values;
    }

    return null;
}
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121