List of usage examples for java.util Comparator Comparator
Comparator
From source file:Main.java
public static <K, V> Map<K, V> sortMapByValue(Map<K, V> map, final Boolean asc) { List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet()); Collections.sort(entries, new Comparator<Entry<K, V>>() { @SuppressWarnings("unchecked") public int compare(Entry<K, V> o1, Entry<K, V> o2) { if (asc) { return ((Comparable<V>) o1.getValue()).compareTo(o2.getValue()); } else { return -((Comparable<V>) o1.getValue()).compareTo(o2.getValue()); }//from w w w .j ava2 s.c om } }); Map<K, V> result = new LinkedHashMap<K, V>(); for (Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
private static List<String> getSortedGPXFilenames(File dir, String sub) { final List<String> list = new ArrayList<String>(); readGpxDirectory(dir, list, ""); Collections.sort(list, new Comparator<String>() { @Override/* ww w.j ava 2 s .com*/ public int compare(String object1, String object2) { if (object1.compareTo(object2) > 0) { return -1; } else if (object1.equals(object2)) { return 0; } return 1; } }); return list; }
From source file:Main.java
private static List<ThreadInfo> sort(ThreadInfo[] dumpAllThreads) { List<ThreadInfo> allThreads = new ArrayList<ThreadInfo>(Arrays.asList(dumpAllThreads)); Collections.sort(allThreads, new Comparator<ThreadInfo>() { @Override// w w w . j a va2 s . c o m public int compare(ThreadInfo o1, ThreadInfo o2) { return o1.getThreadName().compareTo(o2.getThreadName()); } }); return allThreads; }
From source file:Main.java
@Nonnull public static <T> Comparator<Collection<? extends T>> setComparator( @Nonnull final Comparator<? super T> elementComparator) { return new Comparator<Collection<? extends T>>() { @Override//from w ww .j a v a 2 s . co m public int compare(Collection<? extends T> list1, Collection<? extends T> list2) { return compareAsSet(elementComparator, list1, list2); } }; }
From source file:Main.java
/** * Sorts entry set by values in ascending order. * // w ww. j a v a 2s.c o m * @param map * @return sorted entry set. */ public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> sortByValuesAscending( Map<K, V> map) { SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) { int result = e1.getValue().compareTo(e2.getValue()); return result != 0 ? result : 1; // saves equal entries } }); sortedEntries.addAll(map.entrySet()); return sortedEntries; }
From source file:Main.java
/** * Sorts entry set by values in descending order. * // w w w. j a v a 2 s . c o m * @param map * @return sorted entry set. */ public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> sortByValuesDescending( Map<K, V> map) { SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) { int result = e2.getValue().compareTo(e1.getValue()); return result != 0 ? result : 1; // saves equal entries } }); sortedEntries.addAll(map.entrySet()); return sortedEntries; }
From source file:Main.java
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { public int compare(Entry<K, V> o1, Entry<K, V> o2) { return (o2.getValue()).compareTo(o1.getValue()); }/*from w w w.ja v a 2 s. c o m*/ }); Map<K, V> result = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
public static List<String> getSortedStringList(Collection<String> toSort) { List<String> sorted = new LinkedList<String>(); final PriorityQueue<String> ordered = new PriorityQueue<String>( toSort.size() + 1/*In case the toSort is empty*/, new Comparator<String>() { @Override//from ww w .j a v a 2 s . c o m public int compare(String lhs, String rhs) { lhs = lhs.replaceAll("[^a-zA-Z0-9]", ""); rhs = rhs.replaceAll("[^a-zA-Z0-9]", ""); int result = rhs.compareTo(lhs); return result; } }); ordered.addAll(toSort); int originalSize = ordered.size(); for (int i = 0; i < originalSize; i++) { sorted.add(ordered.poll()); } return sorted; }
From source file:Main.java
/** * Create a comparator that compares against the distance from the specified point. * * Note: The comparator will continue to sort by distance from the origin point, even if the * origin point's coordinates are modified after the comparator is created. * * Used by positionRect().// w ww . j a va 2 s. c o m */ public static <P extends Point2D> Comparator<P> createPointComparator(final P origin) { return new Comparator<P>() { public int compare(P p1, P p2) { double dist1 = origin.distance(p1); double dist2 = origin.distance(p2); return (dist1 > dist2) ? 1 : ((dist1 < dist2) ? -1 : 0); } }; }
From source file:Main.java
@SuppressWarnings("unchecked") public static HashMap<?, ?> sortByValue(HashMap<?, ?> map, final int flag) { // flag = 0 decreasing order otherwise increasing List list = new LinkedList(map.entrySet()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { if (flag == 0) return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue()); else/*w w w .j av a 2s.com*/ return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); } }); HashMap result = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); result.put(entry.getKey(), entry.getValue()); } return result; }