How to Sort a Char Array in Java

How to Sort a Char Array in Java | Now, we will sort a character array by using the sort() method available in the Java arrays class. Method details is as follows:- public static void sort(int[ ] array, int start_Index, int end_Index) ;

Parameters:- array, start index, and end index.
a) array:- the array that is needed to be sorted.
b) start_index:- the index from where the sorting should be started.
c) end_Index:- the index where sorting should be stopped.

This sorts only the specified index, we can use this for selective sorting. Syntax:-public static void sort(char[ ] array)
Parameters:- array
The whole array which is needed to be sorted, this sorts the complete array.

Java Program to Sort a Char Array in Java

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      char[] charArray = { 'Z', 'B', 'S', 'A', 'C' };
      System.out.println("Character Array Before Sorting: " 
                         + Arrays.toString(charArray));

      Arrays.sort(charArray);
      System.out.println("Character Array After Sorting: " 
                         + Arrays.toString(charArray));
   }
}

Output:

Character Array Before Sorting: [Z, B, S, A, C]
Character Array After Sorting: [A, B, C, S, Z]

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      char[] charArray = { 'D', 'F', 'V', 'J', 'U', 'M', 'C' };
      System.out.println("Character Array Before Selective " +  
                     "Sorting: " + Arrays.toString(charArray));

      Arrays.sort(charArray, 2, 5);
      System.out.println("After Selective Sorting: " 
                         + Arrays.toString(charArray));
   }
}

Output:-

Character Array Before Selective Sorting: [D, F, V, J, U, M, C]
After Selective Sorting: [D, F, J, U, V, M, C]

The above program sorts the given character array only in between the given range. The sorting starts from the 2nd index and ends before the 5th index. It sorts char arrays in the ascending order.

How To Sort Character Array In Java

Now let us see a demonstration of both methods in a single program to sort a char array in Java.

import java.util.Arrays;

public class Main {

   public static void main(String[] args) {
      char[] array = { 'a', 'z', 'v', 'b', 'f', 'g' };
      System.out.println("Original Array : " + 
                         Arrays.toString(array));

      Arrays.sort(array);
      System.out.println("Sorted char array : " + 
                         Arrays.toString(array));

      char[] array2 = { 'a', 'z', 'v', 'b', 'f', 'g' };
      Arrays.sort(array2, 1, 4);
      System.out.println("Partially Sorted char array: " +
                         Arrays.toString(array2));
   }
}

Output:

Original Array : [a, z, v, b, f, g]
Sorted char array : [a, b, f, g, v, z]
Partially Sorted char array: [a, b, v, z, f, g]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *