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

/**
 * Sort a map by supplied comparator logic.
 *
 * @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.
 * @author Maxim Veksler//from   ww w.j  a  v a 2s . c o  m
 */
public static <K, V> LinkedHashMap<K, V> sortMap(final Map<K, V> map,
        final Comparator<Map.Entry<K, V>> comparator) {
    // Convert the map into a list of key,value pairs.
    List<Map.Entry<K, V>> mapEntries = new LinkedList<Map.Entry<K, V>>(map.entrySet());

    // Sort the converted list according to supplied comparator.
    Collections.sort(mapEntries, comparator);

    // Build a new ordered map, containing the same entries as the old map.
    LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(map.size() + (map.size() / 20));
    for (Map.Entry<K, V> entry : mapEntries) {
        // We iterate on the mapEntries list which is sorted by the comparator putting new entries into
        // the targeted result which is a sorted map.
        result.put(entry.getKey(), entry.getValue());
    }

    return result;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortMapByValue(Map<K, V> map,
        final boolean descending) {
    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) {
            int comp = (o1.getValue()).compareTo(o2.getValue());

            if (descending) {
                comp = comp * (-1);//from ww w .jav a  2s.c  o m
            }

            return comp;
        }
    });

    LinkedHashMap<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 <T> void nthElement(int start, int n, int end, List<T> list, Comparator<T> comparator) {
    List<T> newList = new ArrayList<T>();
    for (int i = start; i < end; i++)
        newList.add(list.get(i));/*from   w  w  w . j  a  v a  2  s.  c  o m*/
    Collections.sort(newList, comparator);
    for (int i = start, j = 0; i < end; i++, j++)
        list.set(i, newList.get(j));
}

From source file:Main.java

public static <T> List<T> sort(Collection<T> input, Comparator<T> comparator) {
    List<T> result = Lists.newArrayList(input);
    Collections.sort(result, comparator);
    return result;
}

From source file:Main.java

public static <T, C> Collection<T> sortBy(Collection<T> collection, final Map<T, C> valuesMap) {
    List<T> ordered = new ArrayList<T>(collection);
    Comparator<T> compartor = new Comparator<T>() {

        @SuppressWarnings("unchecked")
        @Override//from  w ww . j  a v a2s. c  o  m
        public int compare(T a, T b) {
            C aValue = valuesMap.get(a);
            C bValue = valuesMap.get(b);
            return ((Comparable<C>) aValue).compareTo(bValue);
        }
    };

    Collections.sort(ordered, compartor);

    return ordered;
}

From source file:Main.java

/**
 * Same as in java.util.Collections, but returns list to allow daisy chaining calls
 *///w w  w .  j a v a2 s  .c  o  m
public static <T> List<T> sort(List<T> list, Comparator<T> comparator) {
    Collections.sort(list, comparator);
    return list;
}

From source file:Main.java

public static <T> List range(List<? extends T> list, T min, T max, Comparator<? super T> comparator) {
    List<? super T> copyList = new ArrayList<T>(list);
    Collections.sort(list, comparator);
    if (copyList.indexOf(min) < copyList.indexOf(max)) {
        return copyList.subList(copyList.indexOf(min), copyList.indexOf(max));
    } else {//  ww  w  .  j a  va  2s .  c  om
        return newArrayList();
    }
}

From source file:Main.java

/**
 * Sort a map according to its values in ascending order when asc=true and i descending order otherwise.
 * @param unsortedMap /* ww  w  . jav  a 2s  . co  m*/
 * @param asc
 * @return
 */
public static Map<String, Integer> sortUsingComparator(Map<String, Integer> unsortedMap, final boolean asc) {

    // Convert Map to List
    List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortedMap.entrySet());

    // Sort a list using custom comparator, to compare the Map values
    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            if (asc) {
                return (o1.getValue()).compareTo(o2.getValue());
            } else {
                return -1 * (o1.getValue()).compareTo(o2.getValue());
            }
        }
    });

    // Convert the sorted map back to a Map
    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
        Map.Entry<String, Integer> entry = it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

From source file:Main.java

public static <T> List<T> range(List<? extends T> list, T min, T max, Comparator<T> comparator) {
    List<T> resultList = new ArrayList<T>();
    for (T elem : list) {
        if (comparator.compare(elem, min) >= 0 && comparator.compare(elem, max) <= 0) {
            resultList.add(elem);//w w w.j a  v a2 s. c  om
        }
    }
    Collections.sort(resultList, comparator);
    return resultList;
}

From source file:Main.java

public static void sortCameraSize(List<Camera.Size> list, boolean desc) {
    if (desc) {/*ww  w .  j a v  a2  s.co  m*/
        Collections.sort(list, _descSize);
    } else {
        Collections.sort(list, _ascSize);
    }
}