1

I have been trying to sort the map in descending order of values but my existing code is sorting in descending order of keys. how do I sort in descending order of values? My code:

System.out.println("Unsorted Map:" +merger);
Map<Integer, Double> Sortedmerger = SortByValue(merger); 
System.out.println("Sorted Map: "+ Sortedmerger);

public static TreeMap<Integer, Double> SortByValue  (Map<Integer, Double> map) {
ValueComparator vc =  new ValueComparator(map);
TreeMap<Integer, Double> sortedMap = new TreeMap<Integer, Double>(Collections.reverseOrder());
        sortedMap.putAll(map);
        return sortedMap;
    }   

class ValueComparator implements Comparator<Double> {

    Map<Integer, Double> map;

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

    public int compare(Double a, Double b) {
        if (map.get(a) >= map.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys 
    }
}

My output:

Unsorted Map:{1=0.53, 2=0.48, 23=0.54, 10=0.47}
Sorted Map: {23=0.54, 10=0.47, 2=0.48, 1=0.53}
  • Use a different data structure, or you could try a custom [`Comparator`](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html). – Elliott Frisch Apr 20 '15 at 03:50
  • A map is thought to be used to have fast access (here O(log n)) if you have the key and want the value behind it. As @ElliottFrisch suggests you'd better use another data structure. – lschuetze Apr 20 '15 at 03:59
  • Take a look at this [related question](http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java?rq=1) and read the comments there, as well as the comments on the highly upvoted answers. – Stuart Marks Apr 20 '15 at 20:18

1 Answers1

2

Your map key type is Integer so you have to use Comparator<Integer> and your compare method should take integer inputs and by that Integer key you can get Double value by map.get(a) and map.get(b). and in TreeMap constructor you have to define comparator class which you have created.(i.e ValueComparator)

refer this code:

public static TreeMap<Integer, Double> SortByValue(Map<Integer, Double> map) {
        ValueComparator vc =  new ValueComparator(map);
        TreeMap<Integer, Double> sortedMap = new TreeMap<Integer, Double>(vc);\\constructor should be vc which extends Comparator
        sortedMap.putAll(map); 
        return sortedMap;
    }

class ValueComparator implements Comparator<Integer> {

    Map<Integer, Double> map;

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

    @Override
    public int compare(Integer a, Integer b) {
        // TODO Auto-generated method stub
        if (map.get(a) >= map.get(b)) {
            return -1;
        } else {
            return 1;
        }
    }
}
Zealous System
  • 2,080
  • 11
  • 22
  • This looks similar to [this answer](http://stackoverflow.com/a/1283722/1441122) to a related question. While it may work in some cases, be aware that it has significant restrictions, which will cause obscure bugs if violated. – Stuart Marks Apr 20 '15 at 20:29