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 <T> List range(List<? extends T> list, T min, T max) {
    List<? super T> copyList = new ArrayList<>(list);
    Collections.sort(copyList, (o1, o2) -> Integer.compare(o1.hashCode(), o2.hashCode()));
    if (copyList.indexOf(min) < copyList.indexOf(max)) {
        return copyList.subList(copyList.indexOf(min), copyList.indexOf(max));
    } else {// w  ww . j a  v  a 2  s  .co  m
        return newArrayList();
    }
}

From source file:Main.java

public static void sortDates(List<DAY> dayList) {
    Collections.sort(dayList, new Comparator<DAY>() {
        public int compare(DAY day1, DAY day2) {
            return day1.getWeight() - day2.getWeight();
        }/*from w w w  . ja  v  a2s. co  m*/
    });
    System.out.println("sortedlist is" + dayList.toString());
}

From source file:Main.java

public static <T> List<T> asSortedList(Collection<T> collection, Comparator<? super T> comparator) {
    List<T> list = new ArrayList<T>(collection);
    Collections.sort(list, comparator);

    return list;/*ww w. j  a  v a 2  s.c o  m*/
}

From source file:Main.java

public static <T> void sort(List<T> list, Comparator<? super T> c) {
    Collections.sort(list, c);
}

From source file:Main.java

public static <T> void sort(java.util.List<T> list, Comparator<? super T> comparator) {
    if (list == null || list.size() < 2) {
        return; // nothing to sort if null / empty / 1 element
    }/*from w ww .java  2  s .  co  m*/
    Collections.sort(list, comparator);
}

From source file:Main.java

public static <E> List<E> sort(Collection<E> c, Comparator<E> r) {
    List<E> list = new ArrayList<E>(c);
    Collections.sort(list, r);
    return list;/*from   w w  w .j  av a 2s .  c  o m*/
}

From source file:Main.java

public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
    List list = new LinkedList(map.entrySet());
    Collections.sort(list, new Comparator() {

        @Override/*from   ww  w . j  a  v  a2 s. c o  m*/
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
        }
    });

    Map 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> List<Map.Entry<K, V>> sortMapByValue(HashMap<K, V> map, final int sort) {
    List<Map.Entry<K, V>> orderList = new ArrayList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(orderList, new Comparator<Map.Entry<K, V>>() {
        @Override/*  w ww  .  j  a va2  s  .  c  om*/
        @SuppressWarnings("unchecked")
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (((Comparable<V>) o2.getValue()).compareTo(o1.getValue())) * sort;
        }
    });
    return orderList;
}

From source file:Main.java

public static <T extends Comparable> List<Entry<String, T>> sortMapAsc(Map<String, T> keywordMap) {
    List<Entry<String, T>> arrayList = new ArrayList<Entry<String, T>>(keywordMap.entrySet());
    Collections.sort(arrayList, new Comparator<Entry<String, T>>() {

        @Override//  ww  w  .j a v a 2s.c om
        public int compare(Entry<String, T> e1, Entry<String, T> e2) {
            return (e1.getValue()).compareTo(e2.getValue());
        }
    });
    return arrayList;
}

From source file:Main.java

public static List<Map.Entry<String, Double>> sortMapByValue(Map<String, Double> map) {
    List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
        @Override//w  ww .  ja  va2  s .c o m
        public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
            double result = o2.getValue() - o1.getValue();
            if (result > 0)
                return -1;
            else if (result == 0)
                return 0;
            else
                return 1;
        }
    });
    return list;
}