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:MainClass.java

License:asdf

public static void main(String[] args) {
    String[] sa = new String[] { "adf", "fdsa", "ASDF", "FSA", "r", "R" };

    System.out.println("Before sorting: " + Arrays.asList(sa));
    Arrays.sort(sa, new AlphabeticComparator());
    System.out.println("After sorting: " + Arrays.asList(sa));
}

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("\nStrings that contain ?e? first, everything else second: ");
    Arrays.sort(array, (s1, s2) -> {
        if (s1.contains("e") && s2.contains("e")) {
            return 0;
        } else if (s1.contains("e")) {
            return -1;
        } else {//from w  w w.  ja v  a2  s  . com
            return 1;
        }
    });
    Arrays.asList(array).forEach(System.out::println);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Comparator comp = Collections.reverseOrder();
    String[] a = new String[] { "a", "b", "c" };
    Arrays.sort(a, comp);
    for (int i = 0, n = a.length; i < n; i++) {
        System.out.println(a[i]);
    }//w  w w.j a  v  a 2 s.  c  o m
}

From source file:Main.java

public static void main(String[] args) {
    final String dir = "E:\\Java_Dev\\";
    final File file = Paths.get(dir).toFile();
    File[] files = file.listFiles();
    Arrays.sort(files, (f1, f2) -> {
        if (f1.isDirectory() != f2.isDirectory()) {
            if (f1.isDirectory()) {
                return -1;
            } else {
                return 1;
            }/* w w w  .  jav a2 s . c  o  m*/
        } else {
            return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
        }
    });
    Stream.of(files).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    String[] testArray = { "/", "/games/", "/homework/", "/usr/", "/games/snake/", "/temp/downloads/",
            "/usr/local/", "/usr/local/bin/" };

    Comparator<String> pathComparator = new PathComparator();
    Arrays.sort(testArray, pathComparator);
    System.out.println(Arrays.toString(testArray));
}

From source file:MyComparator.java

public static void main(String[] argv) throws Exception {
    String strs[] = { "d", "h", "a", "c", "t" };

    MyComparator rsc = new MyComparator();

    Arrays.sort(strs, rsc);

    for (String s : strs)
        System.out.println(s + " ");

    Arrays.sort(strs);/*w  ww  .jav  a  2 s.co  m*/

    System.out.print("Sorted in natural order: ");
    for (String s : strs)
        System.out.println(s + " ");
}

From source file:MyComparator.java

public static void main(String[] argv) throws Exception {
    String strs[] = { "a", "G", "g", "b", };

    MyComparator icc = new MyComparator();

    Arrays.sort(strs, icc);

    for (String s : strs) {
        System.out.println(s + " ");
    }/*from   w  w w. j  av  a2s  .c  o m*/
    Arrays.sort(strs);

    System.out.print("Default, case-sensitive sorted order: ");
    for (String s : strs) {
        System.out.println(s + " ");
    }
}

From source file:AlphabeticSearch.java

public static void main(String[] args) {
    String[] sa = new String[] { "a", "c", "d" };
    AlphabeticComparator comp = new AlphabeticComparator();
    Arrays.sort(sa, comp);
    int index = Arrays.binarySearch(sa, sa[10], comp);
    System.out.println("Index = " + index);
}

From source file:AlphabeticSorting.java

public static void main(String[] args) {
    String[] sa = new String[] { "a", "c", "b" };

    System.out.println("Before sorting: " + Arrays.asList(sa));
    Arrays.sort(sa, new AlphabeticComparator());
    System.out.println("After sorting: " + Arrays.asList(sa));
}

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted short array
    Short[] sArr = new Short[] { 3, 13, 1, 9, 21 };

    System.out.println(Arrays.toString(sArr));

    // create a comparator
    Comparator<Short> comp = Collections.reverseOrder();

    // sorting array with reverse order using comparator
    Arrays.sort(sArr, comp);

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