summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/src/kz/ilotterytea/javaextra/comparators/MapValueKeyComparator.java20
1 files changed, 20 insertions, 0 deletions
diff --git a/core/src/kz/ilotterytea/javaextra/comparators/MapValueKeyComparator.java b/core/src/kz/ilotterytea/javaextra/comparators/MapValueKeyComparator.java
new file mode 100644
index 0000000..d168cf8
--- /dev/null
+++ b/core/src/kz/ilotterytea/javaextra/comparators/MapValueKeyComparator.java
@@ -0,0 +1,20 @@
+package kz.ilotterytea.javaextra.comparators;
+
+import java.util.Comparator;
+import java.util.Map;
+
+
+// Cv pasted from https://stackoverflow.com/a/53081966
+public class MapValueKeyComparator<K extends Comparable<? super K>, V extends Comparable<? super V>>
+ implements Comparator<Map.Entry<K, V>> {
+
+ public int compare(Map.Entry<K, V> a, Map.Entry<K, V> b) {
+ int cmp1 = b.getValue().compareTo(a.getValue()); //can reverse a and b position for ascending/descending ordering
+ if (cmp1 != 0) {
+ return cmp1;
+ } else {
+ return a.getKey().compareTo(b.getKey()); //can reverse a and b position for ascending/descending ordering
+ }
+ }
+
+}