Jeeppp
Jeeppp

Reputation: 1573

sorting on map based on values

How to sort a hash map based on values and if the values are same then the sorting should be on the key.

I tried to use a comparator, but its not giving expected results.

I want the result to be like this

{Bajaj=8.0, Tata=7.99, Maruthi=6.34, Kmart=5.99, Honda=5.78, 
Adidas=4.99, Ford=3.99, Nike=3.99, Sears=3.99, Suzuki=3.99, 
Apple=2.99, Puma=1.99}

Here's the complete source code:

import java.util.*;

public class Test {
    public static void main(String[] args) {

        HashMap<String, Double> map = new HashMap<String, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

        map.put("Adidas", 4.99);
        map.put("Nike", 3.99);
        map.put("Puma", 1.99);
        map.put("Ford", 3.99);
        map.put("Apple", 2.99);
        map.put("Sears", 3.99);
        map.put("Kmart", 5.99);
        map.put("Tata", 7.99);
        map.put("Maruthi", 6.34);
        map.put("Honda", 5.78);
        map.put("Bajaj", 8.0);
        map.put("Suzuki", 3.99);

        System.out.println("unsorted map: " + map);

        sorted_map.putAll(map);

        System.out.println("results: " + sorted_map);
    }
}

class ValueComparator implements Comparator<String> {

    Map<String, Double> base;

    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with
    // equals.
    @Override
    public int compare(String a, String b) {
        if (base.get(a) > base.get(b)) {
            return -1;
        } else if (base.get(a) == base.get(b)) {
            System.out.println();
            if (a.compareTo(b) == -1) {
                return -1;
            } else if (a.compareTo(b) == 1) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}

Upvotes: 1

Views: 135

Answers (3)

Kulin Soni
Kulin Soni

Reputation: 74

Just little bit twisted your compare method of ValueComparator class. This will first sort on values & if values are same then sorting on keys. Hope this helps. Output would be something like below:

unsorted map: {Adidas=4.99, Bajaj=8.0, Apple=2.99, Ford=3.99, Puma=1.99, Tata=7.99, Nike=3.99, Suzuki=3.99, Honda=5.78, Kmart=5.99, Maruthi=6.34, Sears=3.99}

results: {Bajaj=8.0, Tata=7.99, Maruthi=6.34, Kmart=5.99, Honda=5.78, Adidas=4.99, Ford=3.99, Nike=3.99, Sears=3.99, Suzuki=3.99, Apple=2.99, Puma=1.99}

import java.util.*;

public class SortValueMap {

    public static void main(String[] args) {

        HashMap<String, Double> map = new HashMap<String, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

        map.put("Adidas", 4.99);
        map.put("Nike", 3.99);
        map.put("Puma", 1.99);
        map.put("Ford", 3.99);
        map.put("Apple", 2.99);
        map.put("Sears", 3.99);
        map.put("Kmart", 5.99);
        map.put("Tata", 7.99);
        map.put("Maruthi", 6.34);
        map.put("Honda", 5.78);
        map.put("Bajaj", 8.0);
        map.put("Suzuki", 3.99);

        System.out.println("unsorted map: " + map);

        sorted_map.putAll(map);

        System.out.println("results: " + sorted_map);
    }
}

class ValueComparator implements Comparator<String> {

    Map<String, Double> base;

    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with
    // equals.
    @Override
    public int compare(String a, String b) {
        if(base.get(a).compareTo(base.get(b)) != 0) {
            if (base.get(a) > base.get(b)) {
                return -1;
            } else { 
                return 1;
            }
        }
        return a.compareTo(b);
    }
}

Upvotes: 0

Tagir Valeev
Tagir Valeev

Reputation: 100149

Here you have two problems in your compare implementation. First, you compare boxed Double values with ==:

else if(base.get(a) == base.get(b))

You should replace this with

else if(base.get(a).equals(base.get(b)))

Second, you check a.compareTo(b) for specific values like -1 and 1, but it may return any positive/negative numbers. It's better and simpler just to return the result of a.compareTo(b) instead. Here's the fixed compare method:

public int compare(String a, String b) {
    if (base.get(a) > base.get(b)) {
        return -1;
    } else if (base.get(a).equals(base.get(b))) {
        return a.compareTo(b);
    } else {
        return 1;
    } // returning 0 would merge keys
}

If you want to sort the keys with the same value in case-insensitive manner, just use compareToIgnoreCase:

public int compare(String a, String b) {
    if (base.get(a) > base.get(b)) {
        return -1;
    } else if (base.get(a).equals(base.get(b))) {
        return a.compareToIgnoreCase(b);
    } else {
        return 1;
    } // returning 0 would merge keys
}

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109547

As the sorting order relies on both value and key, use the Map.Entry<String, Double> entries:

List<Map.Entry<String, Double>> entries = new ArrayList<>(base.entrySet());
Collections.sort(entries, new Comparator<Map<String, Double>>() {
    ...
});

Upvotes: 0

Related Questions