Java Map Sort sortByValue(final Map map)

Here you can find the source of sortByValue(final Map map)

Description

sort By Value

License

Open Source License

Declaration

public static List<Integer> sortByValue(final Map<Integer, Double> map) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;

import java.util.List;
import java.util.Map;

public class Main {
    public static List<Integer> sortByValue(final Map<Integer, Double> map) {
        List<Integer> list = new ArrayList<Integer>();
        list.addAll(map.keySet());/*from  w  w w. j  av a 2s  . c  o  m*/

        Collections.sort(list, new Comparator<Object>() {

            @SuppressWarnings("unchecked")
            public int compare(Object o1, Object o2) {
                Object v1 = map.get(o1);
                Object v2 = map.get(o2);

                return ((Comparable<Object>) v1).compareTo(v2);
            }

        });
        Collections.reverse(list); // if commented, asc
        return list;
    }
}

Related

  1. sortByMap(Map map, int size)
  2. sortByValue( final Map map)
  3. sortByValue( Map map)
  4. sortByValue( Map map, final boolean ascendingValues)
  5. sortByValue(final Map m)
  6. sortByValue(final Map map)
  7. sortByValue(final Map map, final boolean asc)
  8. sortByValue(Map map)
  9. sortByValue(Map map)