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 void main(String[] args) {
    List<String> times = Arrays.asList(new String[] { "03:00 AM", "12:30 PM", "16:15" });
    Collections.sort(times, new TimeStringComparator());
    System.out.println(times);/*from  w  ww. j  a  v a2s.  co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
    List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));

    Comparator<Integer> normal = Integer::compare;
    Comparator<Integer> reversed = normal.reversed();
    Collections.sort(listOfIntegers, reversed);

    listOfIntegers.stream().forEach(e -> System.out.print(e + " "));
    System.out.println("");

}

From source file:Main.java

public static void main(String[] args) {
    List<String> fruits = new ArrayList<String>();
    fruits.add("A");
    fruits.add("");
    fruits.add("C");
    fruits.add("D");
    fruits.add("A");

    Collator collator = Collator.getInstance(Locale.US);

    Collections.sort(fruits, collator);

    for (int i = 0; i < fruits.size(); i++) {
        String fruit = fruits.get(i);

        System.out.println("Fruit = " + fruit);
    }/*from  www.  j  av  a  2 s  .  c  o  m*/
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");

    Comparator comparator = Collections.reverseOrder();
    System.out.println(arrayList);

    Collections.sort(arrayList, comparator);
    System.out.println(arrayList);
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("java2s.com");

    Comparator comparator = Collections.reverseOrder();
    System.out.println(arrayList);

    Collections.sort(arrayList, comparator);
    System.out.println(arrayList);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    table.setAutoCreateColumnsFromModel(false);

    Vector data = model.getDataVector();
    Collections.sort(data, new ColumnSorter(0));
    model.fireTableStructureChanged();// w  w w  .  j av a 2 s .c  o m
}

From source file:MainClass.java

public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add("\u00e4pple");
    list.add("banan");
    list.add("p\u00e4ron");
    list.add("orange");

    // Obtain a Swedish collator
    Collator collate = Collator.getInstance(new Locale("sv", ""));
    Collections.sort(list, collate);

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }//  ww w  . j  a va2  s.  c  om
}

From source file:MainClass.java

public static void main(String args[]) {
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);//from   w  w  w. ja  v a 2  s  .  c om
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    System.out.print("List sorted in reverse: ");
    for (int i : ll) {
        System.out.print(i + " ");
    }

    System.out.println();

    Collections.shuffle(ll);

    System.out.print("List shuffled: ");
    for (int i : ll)
        System.out.print(i + " ");

    System.out.println();

    System.out.println("Minimum: " + Collections.min(ll));
    System.out.println("Maximum: " + Collections.max(ll));
}

From source file:Main.java

public static void main(String args[]) {
    // create linked list object        
    List<Integer> list = new LinkedList<Integer>();

    // populate the list 
    list.add(-8);/*from w w w  . ja  va  2 s. c o m*/
    list.add(2);
    list.add(-2);
    list.add(8);

    // create comparator for reverse order
    Comparator<Integer> cmp = Collections.reverseOrder();

    // sort the list
    Collections.sort(list, cmp);

    System.out.println("List sorted in ReverseOrder: ");
    System.out.println(list);

}

From source file:Main.java

public static void main(String args[]) {
    // create linked list object        
    List<Integer> list = new LinkedList<Integer>();

    // populate the list 
    list.add(-2);//from   w  w w  .j  a va  2 s .c o m
    list.add(2);
    list.add(-12);
    list.add(8);

    // create comparator for reverse order
    Comparator cmp = Collections.reverseOrder(Collections.reverseOrder());

    // sort the list
    Collections.sort(list, cmp);

    System.out.println(list);

}