Java Map Sort sortKeysByValue(final Map m)

Here you can find the source of sortKeysByValue(final Map m)

Description

a generic method that returns the list of keys sorted by value.

License

Open Source License

Parameter

Parameter Description

Declaration

public static java.util.List<Object> sortKeysByValue(final Map<Object, Object> m) 

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.Map;

public class Main {
    /**/*from   w  ww. j a va2 s.  co  m*/
     * a generic method that returns the list of keys sorted by value.
     * @param
     * @return
     */
    public static java.util.List<Object> sortKeysByValue(final Map<Object, Object> m) {
        java.util.List<Object> keys = new ArrayList<Object>();
        keys.addAll(m.keySet());
        Collections.sort(keys, new Comparator<Object>() {
            public int compare(Object key1, Object key2) {
                Object v1 = m.get(key1);
                Object v2 = m.get(key2);
                if (v1 == null) {
                    return (v2 == null) ? 0 : 1;
                } else if (v1 instanceof Comparable) {
                    return ((Comparable<Object>) v1).compareTo(v2);
                } else {
                    return 0;
                }
            }
        });
        return keys;
    }
}

Related

  1. sortedTable(Map map)
  2. sortedView(Map original, int numItems)
  3. sortEntries(Map map, Comparator> comparator)
  4. sortHashMapByValuesD(HashMap passedMap)
  5. sortingHelperSessions(String column, Map sortParams)
  6. sortKeysDesc(HashMap> hash)
  7. sortKeyValuePairByValue( Map map)
  8. sortMap(final Map map, final Comparator> comparator)
  9. sortMap(LinkedHashMap map, Comparator> c)