Example usage for java.util Collections sort

List of usage examples for java.util Collections sort

Introduction

In this page you can find the example usage for java.util Collections sort.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> void sort(List<T> list, Comparator<? super T> c) 

Source Link

Document

Sorts the specified list according to the order induced by the specified comparator.

Usage

From source file:Main.java

public static List sortMapByKeyASC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey());
        }/*from   w  w w  .  ja  v  a 2s  .com*/
    });

    return list;
}

From source file:Main.java

public static List sortMapByKeyDESC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getKey()).compareTo(((Map.Entry) (o1)).getKey());
        }// w w  w.  j  a va  2 s  .  c  om
    });

    return list;
}

From source file:Main.java

public static List sortMapByValueASC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
        }//w w w . ja  va2 s.  com
    });

    return list;
}

From source file:Main.java

public static List sortMapByValueDESC(Map map) {
    List list = new ArrayList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
        }/*from w  w w. j  a  v  a2  s .c  o  m*/
    });

    return list;
}

From source file:Main.java

public static <T> void sortWithIndexesComparator(final List<T> list, final Comparator<Integer> comparator) {
    Collections.sort(list, new Comparator<T>() {
        @Override//from  w  ww. j  av  a  2  s  .co m
        public int compare(T lhs, T rhs) {
            return comparator.compare(list.indexOf(lhs), list.indexOf(rhs));
        }
    });
}

From source file:Main.java

public static <T> List<T> sort(Collection<T> collection, Comparator<T> comparator) {
    List<T> list = new ArrayList<>(collection);
    Collections.sort(list, comparator);
    return list;/*w ww  . j a v  a 2 s  .c  o m*/
}

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(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o2.getValue()).compareTo(o1.getValue());
        }/*from ww  w . j  av  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<Map.Entry<Long, Long>> sortEntrySetToList(Set<Map.Entry<Long, Long>> set) {
    List<Map.Entry<Long, Long>> list = new LinkedList<>(set);
    Collections.sort(list, new Comparator<Map.Entry<Long, Long>>() {

        @Override//from   w w  w .  j a  va  2s.  com
        public int compare(Map.Entry<Long, Long> o1, Map.Entry<Long, Long> o2) {
            if (o1.getValue() > o2.getValue()) {
                return 1;
            }
            if (o1.getValue() < o2.getValue()) {
                return -1;
            }
            return 0;
        }
    });
    return list;
}

From source file:Main.java

public static List<Map.Entry<Long, Long>> sortEntrySetToList(Set<Map.Entry<Long, Long>> set) {
    List<Map.Entry<Long, Long>> list = new LinkedList<Map.Entry<Long, Long>>(set);
    Collections.sort(list, new Comparator<Map.Entry<Long, Long>>() {

        @Override/*from  w w  w .j  a  va2 s  . co  m*/
        public int compare(Map.Entry<Long, Long> o1, Map.Entry<Long, Long> o2) {
            if (o1.getValue() > o2.getValue())
                return 1;
            if (o1.getValue() < o2.getValue())
                return -1;
            return 0;
        }
    });
    return list;
}

From source file:Main.java

public static List<Entry<Long, Long>> sortEntrySetToList(Set<Entry<Long, Long>> set) {
    List<Entry<Long, Long>> list = new LinkedList<>(set);
    Collections.sort(list, new Comparator<Entry<Long, Long>>() {

        @Override//from www. j  a va 2  s  .  com
        public int compare(Entry<Long, Long> o1, Entry<Long, Long> o2) {
            if (o1.getValue() > o2.getValue()) {
                return 1;
            }
            if (o1.getValue() < o2.getValue()) {
                return -1;
            }
            return 0;
        }
    });
    return list;
}