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")
public static <T extends Comparable<? super T>> void sort(List<T> list) 

Source Link

Document

Sorts the specified list into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:Main.java

static <T extends Comparable<T>> void nthElement(int start, int n, int end, List<T> list) {
    List<T> newList = new ArrayList<T>();
    for (int i = start; i < end; i++)
        newList.add(list.get(i));//from  w  w w. j av a2s.  c o  m
    Collections.sort(newList);
    for (int i = start, j = 0; i < end; i++, j++)
        list.set(i, newList.get(j));
}

From source file:Main.java

public static List<Integer> sort(List<Integer> aList) {
    List<Integer> tmp = new LinkedList<>(aList);
    Collections.sort(tmp);
    return tmp;/*from   www  . ja v  a2 s.  co  m*/
}

From source file:Main.java

public static <T extends Comparable<? super T>> List<T> range(List<? extends T> list, T min, T max) {
    List<T> resultList = new ArrayList<T>();
    for (T elem : list) {
        if (elem.compareTo(min) >= 0 && elem.compareTo(max) <= 0) {
            resultList.add(elem);/*ww w . j a va  2  s. c  om*/
        }
    }
    Collections.sort(resultList);
    return resultList;
}

From source file:Main.java

/**
 * Convert a {@link Collection} of <T> to a sorted {@link List} of <T>.
 */// w w  w.j a  v a 2 s  . com
public static <T extends Comparable<T>> List<T> asSortedList(final Collection<T> collection) {
    final List<T> list = new ArrayList<>(collection);
    Collections.sort(list);
    return list;
}

From source file:Main.java

/**
 * Returns collection as sorted ArrayList
 * // w  ww.j  a va  2s. co  m
 * @param collection
 * @return sorted list
 */
public static <T extends Comparable<? super T>> List<T> asSortedList(Collection<T> collection) {
    List<T> list = new ArrayList<T>(collection);
    Collections.sort(list);
    return list;
}

From source file:Main.java

private static ArrayList<Double> getExactRatios(int motorTeeth, int[] wheelTeeth) {
    exactRatios.clear();/*from w w  w .ja  v a 2 s  . c om*/
    for (int tooth : wheelTeeth) {
        exactRatios.add((double) tooth / (double) motorTeeth);
    }

    Collections.sort(exactRatios); // sort ascending
    Collections.reverse(exactRatios); // reverse to sort descending
    // now the zeroth element contains the highest gear ratio i.e. the lowest gear index

    return exactRatios;
}

From source file:Main.java

@SuppressWarnings("unchecked")
static public LinkedHashMap<Object, Comparable> sortHashMapByValues(HashMap<Object, Comparable> passedMap) {
    ArrayList mapKeys = new ArrayList(passedMap.keySet());
    ArrayList mapValues = new ArrayList(passedMap.values());
    Collections.sort(mapValues);
    Collections.sort(mapKeys);/*from ww  w .  j  a  va2s .c o  m*/

    LinkedHashMap<Object, Comparable> sortedMap = new LinkedHashMap<Object, Comparable>();

    Iterator<Comparable> valueIt = mapValues.iterator();
    while (valueIt.hasNext()) {
        Comparable val = valueIt.next();
        Iterator keyIt = mapKeys.iterator();

        while (keyIt.hasNext()) {
            Object key = keyIt.next();
            Comparable comp = passedMap.get(key);

            if (comp.equals(val)) {
                passedMap.remove(key);
                mapKeys.remove(key);
                sortedMap.put(key, val);
                break;
            }

        }
    }
    return sortedMap;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Convenience method for sorting a list by either ascending or descending
 * Ascending is exactly the same as calling Collections.sort(list)
 * Descending is exactly the same as calling Collections.sort(list, Collections.reverseOrder())
 * /*from ww  w .  j av  a2  s.  com*/
 * @param list
 * @param isAscending
 */
public static <T extends Comparable<? super T>> void sort(List<T> list, boolean isAscending) {
    if (isAscending) {
        Collections.sort(list);
    } else { //is dec
        Collections.sort(list, Collections.reverseOrder());
    }
}

From source file:Main.java

/**
 * Sort a collection of objects and then return the a new sorted list so
 * we can keep things functional.  This also works on Collection types that
 * can't easily be sorted (sets, etc)./*from w w  w. ja v  a 2  s .co m*/
 *
 */
public static <T extends Comparable<T>> List<T> sort(Collection<T> input) {
    List<T> result = Lists.newArrayList(input);
    Collections.sort(result);
    return result;
}