Example usage for java.util Arrays sort

List of usage examples for java.util Arrays sort

Introduction

In this page you can find the example usage for java.util Arrays sort.

Prototype

public static <T> void sort(T[] a, Comparator<? super T> c) 

Source Link

Document

Sorts the specified array of objects according to the order induced by the specified comparator.

Usage

From source file:Main.java

public static void main(String[] args) {
    Integer[] numbers = { 1, 2, 3 };

    Arrays.sort(numbers, (i1, i2) -> {
        return (i2 - i1);
    });/*from  w  w w  .  ja va 2  s  .  c o m*/

    System.out.println(Arrays.toString(numbers));
}

From source file:Main.java

public static void main(String args[]) {

    String[] myArray = new String[] { "A", "B", "b" };
    Arrays.sort(myArray, Collator.getInstance());

    System.out.println(Arrays.toString(myArray));
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] numbers = new Integer[] { 2, 7, 3, 9, 5, 4, 6, 8, 1 };
    Arrays.sort(numbers, Integer::compareTo);
    Stream.of(numbers).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] numbers = { 1, 2, 3 };

    // watch return types, it is implied, no return keyword is necessary
    Arrays.sort(numbers, (i1, i2) -> i2 - i1);

    System.out.println(Arrays.toString(numbers));
}

From source file:Main.java

public static void main(String[] argv) {
    String[] rawStrings = { "B", "java2s.com", "b", "A", "a", "c", "C" };
    System.out.println(Arrays.toString(rawStrings));
    Arrays.sort(rawStrings, String.CASE_INSENSITIVE_ORDER);
    System.out.println(Arrays.toString(rawStrings));

}

From source file:Main.java

public static void main(String[] args) {
    String[] strings = { "Here", "are", "some", "sample", "strings", "to", "be", "sorted" };

    Arrays.sort(strings, new Comparator<String>() {
        public int compare(String s1, String s2) {
            int c = s2.length() - s1.length();
            if (c == 0)
                c = s1.compareToIgnoreCase(s2);
            return c;
        }// w  ww  .  j  a v  a2  s.  c om
    });

    for (String s : strings)
        System.out.print(s + " ");
}

From source file:Main.java

public static void main(String[] args) {
    String[] k1 = { "0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1", "2.10", "2", "2.2", "2.1" };
    Arrays.sort(k1, new VersionNumberComparator());
    System.out.println(Arrays.asList(k1));
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] arrayToSort = new Integer[] { new Integer(5), new Integer(89), new Integer(16), new Integer(2) };

    Arrays.sort(arrayToSort, Collections.reverseOrder());

    for (Integer i : arrayToSort) {
        System.out.println(i.intValue());
    }/*from w w w .j a v a  2 s.co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    String[] array = { "Java", "C#", "Scala", "Basic", "C++", "Ruby", "Pyton", "Perl", "Haskell", "Jet" };
    System.out.println("Shortest to longest: ");
    Arrays.sort(array, (s1, s2) -> s1.length() - s2.length());
    Arrays.asList(array).forEach(System.out::println);

    System.out.println("\nLongest to shortest: ");
    Arrays.sort(array, (s1, s2) -> (s2.length() - s1.length()));
    Arrays.asList(array).forEach(System.out::println);

    System.out.println("\nAlphabetically by the first character only: ");
    Arrays.sort(array, (s1, s2) -> s1.charAt(0) - s2.charAt(0));
    Arrays.asList(array).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    File dir = new File("c:\\");
    File[] files = dir.listFiles();

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
    for (int i = 0; i < files.length; i++) {
        File file = files[i];//from   www  . jav  a 2s . co  m
        System.out.printf("File %s - %2$tm %2$te,%2$tY%n= ", file.getName(), file.lastModified());
    }

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        System.out.printf("File %s - %2$tm %2$te,%2$tY%n= ", file.getName(), file.lastModified());
    }
}