List of usage examples for java.util Comparator Comparator
Comparator
From source file:Main.java
public static String getKeyFromParams(List<NameValuePair> nameValuePairs, String publishId) { Collections.sort(nameValuePairs, new Comparator<NameValuePair>() { @Override/*from ww w . j ava 2s . c om*/ public int compare(NameValuePair p1, NameValuePair p2) { return p1.getName().compareTo(p2.getName()); } }); StringBuilder keyBuilder = new StringBuilder(); boolean isFirst = true; for (NameValuePair nvp : nameValuePairs) { if (!isFirst) { keyBuilder.append("&"); } keyBuilder.append(nvp.getName()).append("=").append(nvp.getValue()); isFirst = false; } keyBuilder.append("&").append(publishId); String key = keyBuilder.toString(); byte[] keyBytes = getBytes(key); return getMd5Digest(new String(Base64.encode(keyBytes, Base64.NO_WRAP))); }
From source file:Main.java
public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> sortMapByValue(Map<K, V> map, final boolean reverse) { if (isEmpty(map)) { return null; }/*from w ww . ja v a 2 s . c o m*/ List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> entry1, Map.Entry<K, V> entry2) { int result = entry1.getValue().compareTo(entry2.getValue()); if (reverse) { result *= -1; } return result; } }); return list; }
From source file:Main.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static HashMap<?, ?> sortByValue(HashMap<?, ?> map, final int flag) { // flag = 0 decreasing order otherwise increasing List<Map.Entry<?, ?>> list = new LinkedList<Map.Entry<?, ?>>(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/*from ww w .j ava 2 s.c om*/ 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; }
From source file:Main.java
public static List<String> getSortedGPXFilenamesByDate(File dir, boolean absolutePath) { final Map<String, Long> mp = new HashMap<String, Long>(); readGpxDirectory(dir, mp, "", absolutePath); ArrayList<String> list = new ArrayList<String>(mp.keySet()); Collections.sort(list, new Comparator<String>() { @Override// ww w. j a v a2s . c o m public int compare(String object1, String object2) { Long l1 = mp.get(object1); Long l2 = mp.get(object2); long lhs = l1 == null ? 0 : l1.longValue(); long rhs = l2 == null ? 0 : l2.longValue(); return lhs < rhs ? 1 : (lhs == rhs ? 0 : -1); } }); return list; }
From source file:Main.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static List<Set<Entry<String, Object>>> sortMapByKey(Map<String, ? extends Object> map) { if (map == null) { return null; }//ww w.j a va 2s . co m List<Set<Entry<String, Object>>> returnlist = new LinkedList(map.entrySet()); Collections.sort(returnlist, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey()); } }); return returnlist; }
From source file:Main.java
public static <T> Comparator<T> identityComparator() { return new Comparator<T>() { @Override//from w w w. j av a 2 s .c o m public int compare(T o1, T o2) { return System.identityHashCode(o1) - System.identityHashCode(o2); } }; }
From source file:Main.java
public static <E, V extends Comparable<? super V>, K> Map<K, V> sortByValueAsc(Map<K, V> map, int limit) { 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(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return (o1.getValue()).compareTo(o2.getValue()); }/*from w w w . j a v a 2 s . co m*/ }); Map<K, V> result = new LinkedHashMap<K, V>(); int counter = 0; for (Map.Entry<K, V> entry : list) { if (limit > 0 && counter >= limit) { break; } result.put(entry.getKey(), entry.getValue()); counter++; } return result; }
From source file:Main.java
/** * sort Map by key desc/*www. j av a2 s .c o m*/ * * @param unsortMap * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map sortByComparator(Map unsortMap) { List list = new LinkedList(unsortMap.entrySet()); // sort list based on comparator Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey()); } }); // put sorted list into map again Map sortedMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; }
From source file:Main.java
/** * Creates a list of sorted entries by copying them from the entrySet of a * map. They are sorted by the natural order of the value (defined by * V.compareTo(V))// w w w .ja va2s . co m * * @param map * the source map * * @return a newly created sorted list */ public static <K, V extends Comparable<V>> List<Entry<K, V>> orderByValue(Map<K, V> map, final boolean desc) { return order(map, new Comparator<Entry<K, V>>() { public int compare(Entry<K, V> o1, Entry<K, V> o2) { return (desc ? -1 : 1) * o1.getValue().compareTo(o2.getValue()); } }); }
From source file:Main.java
/** * Returns the indices that would sort an array. * @param array Array.// w ww . j a v a 2 s .com * @param ascending Ascending order. * @return Array of indices. */ public static int[] Argsort(final double[] array, final boolean ascending) { Integer[] indexes = new Integer[array.length]; for (int i = 0; i < indexes.length; i++) { indexes[i] = i; } Arrays.sort(indexes, new Comparator<Integer>() { @Override public int compare(final Integer i1, final Integer i2) { return (ascending ? 1 : -1) * Double.compare(array[i1], array[i2]); } }); return asArray(indexes); }