Here you can find the source of sortKeysByValue(final Map
Parameter | Description |
---|
public static java.util.List<Object> sortKeysByValue(final Map<Object, Object> m)
//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; } }