Java Collection How to - Sort an array








Question

We would like to know how to sort an array.

Answer

import java.util.Arrays;
/*from  ww  w .jav a2 s  .co  m*/
public class Main {

  public static void main(String[] args) {
    int[] intArray = new int[] { 4, 5, 9, 0, 3, 5, 6, 2 };

    Arrays.sort(intArray);

    for (int i = 0; i < intArray.length; i++)
      System.out.println(intArray[i]);

    String[] stringArray = new String[] { "D", "E", "A", "C", "B" };

    Arrays.sort(stringArray);

    for (int i = 0; i < stringArray.length; i++)
      System.out.println(stringArray[i]);

  }
}

The code above generates the following result.