Added ordering of results by name as well as keywords

This commit is contained in:
Luka 2019-01-26 15:43:42 +01:00
parent 7ce8b83d16
commit b639e36961
2 changed files with 36 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package data;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Objects;
/*
@ -56,4 +57,13 @@ public interface MultipleHMKeys {
@Override
boolean equals(Object obj);
default int compareTo(MultipleHMKeys othr){
return Comparator.comparing(MultipleHMKeys::getK1)
.thenComparing(MultipleHMKeys::getK2)
.thenComparing(MultipleHMKeys::getK3)
.thenComparing(MultipleHMKeys::getK4)
.thenComparing(MultipleHMKeys::getK5)
.compare(this, othr);
}
}

View File

@ -96,6 +96,24 @@ public class Util {
return m;
}
public class ValueThenKeyComparator<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 = a.getValue().compareTo(b.getValue());
if (cmp1 != 0) {
return cmp1;
} else {
return a.getKey().compareTo(b.getKey());
}
}
}
/**
* Sorts a map in a descending order by value.
*/
@ -122,12 +140,17 @@ public class Util {
limit = map.size();
}
Map<K, V> result = new LinkedHashMap<>();
TimeWatch watch = TimeWatch.start();
Stream<Map.Entry<K, V>> st = map.entrySet().stream();
// sort by alphabet
Map<K, V> alphaSorted = new LinkedHashMap<>();
map.entrySet().stream().sorted((a, b) -> ((MultipleHMKeys)a.getKey()).compareTo((MultipleHMKeys)b.getKey())).limit(limit)
.forEachOrdered(e -> alphaSorted.put(e.getKey(), e.getValue()));
st.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(limit)
Map<K, V> result = new LinkedHashMap<>();
alphaSorted.entrySet().stream().sorted((a, b) -> (int) ((Long) b.getValue() - (Long) a.getValue())).limit(limit)
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
if (Settings.PRINT_LOG) {