Java.util.Arrays.sort(char[]) Method

Advertisements


Description

The java.util.Arrays.sort(char[]) method sorts the specified array of chars into ascending numerical order.

Declaration

Following is the declaration for java.util.Arrays.sort() method

public static void sort(char[] a)

Parameters

  • a -- This is the array to be sorted.

Return Value

This method does not return any value.

Exception

  • NA

Example

The following example shows the usage of java.util.Arrays.sort() method.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {

   public static void main(String[] args) {

    // initializing unsorted char array
    char cArr[] = {'r','q','s','p'};

    // let us print all the values available in list
    for (char value : cArr) {
      System.out.println("Value = " + value);
    }

    // sorting array
    Arrays.sort(cArr);

    // let us print all the values available in list
    System.out.println("The sorted char array is:");
    for (char value : cArr) {
      System.out.println("Value = " + value);
    }
  }
}

Let us compile and run the above program, this will produce the following result:

Value = r
Value = q
Value = s
Value = p
The sorted char array is:
Value = p
Value = q
Value = r
Value = s



Advertisements
Advertisements