Java Utility Library

Java Arrays - sort() Method



The java.util.Arrays.sort() method is used to sort the specified array into ascending numerical order.

Syntax

public static void sort(char[] a)

Parameters

a Specify the array to be sorted.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Arrays.sort() method is used to sort a specified char array.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an unsorted char array
    char MyArr[] = {'o', 'u', 'a', 'i', 'e'};

    //printing array before sorting
    System.out.print("MyArr contains:"); 
    for(char c: MyArr)
      System.out.print(" " + c);

    //sort the array
    Arrays.sort(MyArr);

    //printing array after sorting
    System.out.print("\nMyArr contains:"); 
    for(char c: MyArr)
      System.out.print(" " + c);   
  }
}

The output of the above code will be:

MyArr contains: o u a i e
MyArr contains: a e i o u

❮ Java.util - Arrays