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

public static float getMedian(List<Float> values) {
    Collections.sort(values);
    if (values.size() % 2 == 0) {
        return (values.get(values.size() / 2 - 1) + values.get(values.size() / 2)) / 2;
    } else {/*from   www .j av a 2 s. c o  m*/
        return values.get(values.size() / 2);
    }
}

From source file:Main.java

public static <E extends Comparable<E>> List<E> sort(Collection<E> c) {
    List<E> list = new ArrayList<E>(c);
    Collections.sort(list);
    return list;//w w  w. j a  v  a 2  s . c  o  m
}

From source file:Main.java

public static <T extends Comparable<? super T>> List<T> sortAndReturn(final List<T> list) {
    Collections.sort(list);
    return list;//from w w w.j  a  va 2s. c  om
}

From source file:Main.java

public static <T extends Comparable<T>> List<T> getSortedList(List<T> list) {
    List<T> sortedList = new ArrayList<>(list);
    Collections.sort(sortedList);
    return sortedList;
}

From source file:Main.java

public static String sortAndFlattenNameList(List<String> names) {
    Collections.sort(names);

    StringBuilder sb = new StringBuilder();

    for (String name : names) {
        sb.append(name + ", ");
    }/*from  www  .j a v  a 2 s  .co  m*/

    String flattenedNames = sb.toString();

    if (flattenedNames.length() > 2) {
        // drop the last comma and space
        flattenedNames = flattenedNames.substring(0, flattenedNames.length() - 2);
    }

    return flattenedNames;
}

From source file:Main.java

static <T extends Comparable<T>> T getNthElement(int n, List<T> list) {
    List<T> newList = new ArrayList<T>(list);
    Collections.sort(newList);
    return newList.get(n);
}

From source file:Main.java

public static <T extends Comparable<? super T>> List<T> toSortedList(Collection<T> collection) {
    List<T> list = new LinkedList<>(collection);
    Collections.sort(list);

    return list;/* ww  w.  java  2 s.  co m*/
}

From source file:Main.java

public static <T extends Comparable<? super T>> List<T> sorted(Collection<T> c) {
    ArrayList<T> list = new ArrayList<T>(c);
    Collections.sort(list);
    return list;/*from w  w w .  ja  v a 2  s.  c  o m*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> List<T> sortedIfPossible(Collection<T> collection) {
    List<T> result = new ArrayList<T>(collection);
    try {//from   w  ww .  java 2s .c om
        Collections.sort((List) result);
    } catch (ClassCastException e) {
        // unable to sort, just return the copy
    }
    return result;
}

From source file:Main.java

/**
 * Sort/*from   ww  w .  ja v  a 2s  .co m*/
 * 
 * @param list
 *            List{T}
 * @return List{T}
 */
public static <T extends Comparable<? super T>> List<T> sort(List<T> list) {
    if (list == null) {
        return null;
    }
    Collections.sort(list);
    return list;
}