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 void main(String args[]) {
    List<Character> ll = new LinkedList<Character>();

    for (char n = 'A'; n <= 'Z'; n++)
        ll.add(n);//from www . j a  va  2  s . co  m

    Collections.shuffle(ll);

    for (Character x : ll)
        System.out.print(x + " ");
    Collections.sort(ll);

    for (Character x : ll)
        System.out.print(x + " ");

    System.out.println("Searching for F.");
    int i = Collections.binarySearch(ll, 'F');

    if (i >= 0) {
        System.out.println("Found at index " + i);
        System.out.println("Object is " + ll.get(i));
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> list = new ArrayList<Person>();
    for (int i = 10; i > 0; i--) {
        list.add(new Person(i, "name" + String.valueOf(i), new Date()));
    }/*  ww w.j  a va 2s  .c  o m*/
    System.out.println(list);
    Collections.sort(list);
    System.out.println(list);
}

From source file:Main.java

public static void main(String[] args) {
    Set<String> allZones = ZoneId.getAvailableZoneIds();
    List<String> zoneList = new ArrayList<String>(allZones);
    Collections.sort(zoneList);

    LocalDateTime dt = LocalDateTime.now();
    for (String s : zoneList) {
        ZoneId zone = ZoneId.of(s);
        ZonedDateTime zdt = dt.atZone(zone);
        ZoneOffset offset = zdt.getOffset();
        String out = String.format("%35s %10s%n", zone, offset);
        System.out.println(out);//from ww  w  .ja v  a2  s  .c  o  m
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("J");
    list.add("R");
    list.add("C");
    list.add("X");

    System.out.println("List: " + list);

    // Uses Comparable implementation in String class
    // to sort the list in natural order
    Collections.sort(list);
    System.out.println("Sorted List:  " + list);

}

From source file:Main.java

public static void main(String[] args) {
    List<String> colours = new ArrayList<String>();
    colours.add("red");
    colours.add("green");
    colours.add("blue");
    colours.add("yellow");
    colours.add("cyan");
    colours.add("white");
    colours.add("black");

    Collections.sort(colours);
    System.out.println(Arrays.toString(colours.toArray()));

    Collections.sort(colours, Collections.reverseOrder());
    System.out.println(Arrays.toString(colours.toArray()));
}

From source file:Main.java

public static void main(String args[]) {

    String init[] = { "One", "Two", "Three", "One", "from java2s.com", "Three" };

    // create one list
    List<String> list = new ArrayList<String>(Arrays.asList(init));

    System.out.println("List value before: " + list);

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

    System.out.println("List value after sort: " + list);
}

From source file:Main.java

public static void main(String[] args) {
    List list = new LinkedList();

    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    for (int i = 0; i < months.length; i++) {
        String month = months[i];
        list.add(month);/*from  www.  j  a v a2s.  com*/
    }

    Collections.sort(list);
    System.out.println("Month Names = " + list);
    int index = Collections.binarySearch(list, "October");
    if (index > 0) {
        System.out.println("Found at index = " + index);
        String month = (String) list.get(index);
        System.out.println("Month = " + month);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String simpsons[] = { "B", "H", "L", "M", "H", "M", "R" };

    List list = new ArrayList(Arrays.asList(simpsons));

    // Ensure list sorted
    Collections.sort(list);
    System.out.println("Sorted list: [length: " + list.size() + "]");
    System.out.println(list);//w  ww. j  a  v a  2 s  .co  m

    // Search for element in list
    int index = Collections.binarySearch(list, "M");
    System.out.println("Found M @ " + index);

    // Search for element not in list
    index = Collections.binarySearch(list, "J");
    System.out.println("Didn't find J @ " + index);

}

From source file:Main.java

public static void main(String args[]) {

    Hashtable<String, String> h = new Hashtable<String, String>();
    h.put("a", "b");
    h.put("c", "d");
    h.put("e", "f");

    for (String str : h.keySet()) {
        System.out.println(str);//from  ww  w  . ja  v a  2s .c o  m
    }
    List<String> v = new ArrayList<String>(h.keySet());
    Collections.sort(v);

    for (String str : v) {
        System.out.println(str + " " + (String) h.get(str));
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> l1 = new ArrayList<String>();
    List<String> l2 = new ArrayList<String>();
    l1.add("a");//from   w w  w .j  av a2s  .  com
    l1.add("b");
    l1.add("c");

    l2.add("b");
    l2.add("c");
    l2.add("a");

    if (isSorted(l1)) {
        System.out.println("already sorted");
    } else {
        Collections.sort(l1);
    }
}