List of usage examples for java.util Collections sort
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> void sort(List<T> list, Comparator<? super T> c)
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 .java2s . c o m*/ 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 <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 w w . j a va 2s . com 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 www . j a v a2 s. c o m*/ 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
@SuppressWarnings({ "unchecked", "rawtypes" }) public static List<Set<Entry<String, Object>>> sortMapByKey(Map<String, ? extends Object> map) { if (map == null) { return null; }//from ww w . j av a2 s . com 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 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// w w w .j a va 2 s . 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
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()); }// www .j ava 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
/** * map sort/*from w w w .ja v a 2 s . com*/ * @param map * @param compator * @return */ public static <K, V> Map<K, V> sortMap(Map<K, V> map, Comparator<Entry<K, V>> compator) { Map<K, V> result = new LinkedHashMap<K, V>(); List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet()); Collections.sort(entries, compator); for (Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
public static <O> List<O> sort(List<O> collection, Comparator<? super O> comparator, boolean clone) { if (collection == null) return collection; if (clone) {/*from w w w. j a va 2 s. c o m*/ collection = new ArrayList<O>(collection); } Collections.sort(collection, comparator); return collection; }
From source file:Main.java
/** * sort Map by key desc//from www.ja v a 2s. c om * * @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:MyComparator.java
public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) { List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet()); Collections.sort(list, new MyComparator()); Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Entry<String, Integer> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); }/*w w w . j ava 2 s.c o m*/ return sortedMap; }