-
-
Save scottcagno/4573d02509942548c99c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package misc; | |
import java.util.Comparator; | |
import java.util.Map; | |
import java.util.TreeMap; | |
public class ArrayTools | |
{ | |
private static class ValueComparator<K , V extends Comparable<V>> implements Comparator<K> | |
{ | |
Map<K, V> map; | |
public ValueComparator(Map<K, V> map) { | |
this.map = map; | |
} | |
@Override | |
public int compare(K keyA, K keyB) { | |
Comparable<V> valueA = map.get(keyA); | |
V valueB = map.get(keyB); | |
return valueA.compareTo(valueB); | |
} | |
} | |
public static<K, V extends Comparable<V>> Map<K, V> sortByValue(Map<K, V> unsortedMap) | |
{ | |
Map<K, V> sortedMap = new | |
TreeMap<K, V>(new ValueComparator<K, V>(unsortedMap)); | |
sortedMap.putAll(unsortedMap); | |
return sortedMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment