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 void sort(Object[] a) 

Source Link

Document

Sorts the specified array of objects into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:SearchTest.java

public static void main(String args[]) throws Exception {
    int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };

    // Ensure array sorted
    Arrays.sort(array);
    printArray("Sorted array", array);

    // Search for element in array
    int index = Arrays.binarySearch(array, 2);
    System.out.println("Found 2 at " + index);

    // Search for element not in array
    index = Arrays.binarySearch(array, 1);
    System.out.println("Didn't find 1 at " + index);

    // Insert//from w w  w .  j  a  v  a 2 s. com
    int newIndex = -index - 1;
    array = insertElement(array, 1, newIndex);
    printArray("With 1 added", array);

}

From source file:Main.java

public static void main(String[] args) {
    float[] f1 = new float[] { 3.1f, 2.1f, 5.1f, 4.1f, 1.1f };

    for (float f : f1) {
        System.out.print(" " + f);
    }/*from   ww w  .j  a  va  2s. c o  m*/
    Arrays.sort(f1);

    for (float f : f1) {
        System.out.print(" " + f);
    }

    float[] f2 = new float[] { 5.1f, 2.1f, 3.1f, 1.1f, 4.1f };
    Arrays.sort(f2, 1, 4);

    for (float f : f2) {
        System.out.print(" " + f);
    }
}

From source file:ArraysDemo.java

public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
        array[i] = -3 * i;/*from  w  w  w  .j av a  2  s .  co m*/

    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);

    // Fill and display the array.
    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);

    // Sort and display the array.
    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);

    // Binary search for -9.
    System.out.print("The value -9 is at location ");
    int index = Arrays.binarySearch(array, -9);

    System.out.println(index);
}

From source file:Main.java

public static void main(String[] args) {
    String[] teams = new String[5];
    teams[0] = "M";
    teams[1] = "c";
    teams[2] = "A";
    teams[3] = "l";
    teams[4] = "E";

    Arrays.sort(teams);
    System.out.println(Arrays.toString(teams));
    Arrays.sort(teams, String.CASE_INSENSITIVE_ORDER);
    System.out.println(Arrays.toString(teams));
}

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted byte array
    byte bArr[] = { 10, 12, 7, 35 };

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

    // sorting array
    Arrays.sort(bArr);

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

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted int array
    int iArr[] = { 12, 1, 119, 16, 4 };

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

    // sorting array
    Arrays.sort(iArr);

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

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted Object array
    Object ob[] = { 27, 11, 44, 22, 0 };

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

    // sorting array
    Arrays.sort(ob);

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

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted short array
    short sArr[] = { 212, 1, 19, 2, 5 };

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

    // sorting array
    Arrays.sort(sArr);

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

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted long array
    long lArr[] = { 221, 10, 111, 62, 46 };

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

    // sorting array
    Arrays.sort(lArr);

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

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted double array
    double dArr[] = { 13.2, 1.2, 0.7, 6.2, 4.5 };

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

    // sorting array
    Arrays.sort(dArr);

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