Java: char[] Array sorting with example

In this article, we will discuss how to sort char[] array with example

char:

  • Size is 2 bytes
  • Its range is 0 to 65,536 (unsigned)

Arrays class has various sort methods for sorting different primitive data-types

 

Sorting char[] array:

To sort char[] array, we have 2 variant of sort methods from Arrays class,

Method Signature:

public static void sort(char[] a);

public static void sort(char[] a, int fromIndex, int toIndex);

 

char[] array methods for sorting:

Sort method

Description

sort(char[]); sorts complete char[] array
sort(ch[], sIndex, eIndex); sorts partial char[] array, as per limits start-index & end-index specified in the method arguments

 

Let us move forward to discuss both methods for sorting char[] array

Method 1: complete sorting of char[] array

  • Here, complete array will be sorted

Method Signature:

public static void sort(char[] a);

 

SortingCompleteCharArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingCompleteCharArray {

	public static void main(String[] args) {

		// sample char[] array
		char[] cArray = {'R', 'H', 'C', 'E', 'N', 'B', 'S'};

		// before sorting
		System.out.println("Before sorting : ");
		for(char c : cArray) {
			System.out.print(c + " ");
		}

		// sorting full char[] array
		Arrays.sort(cArray);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(char c : cArray) {
			System.out.print(c + " ");
		}
	}
}

Output:

Before sorting : 
R H C E N B S 

After sorting : 
B C E H N R S 

 

Method 2: partial sorting of char[] array

  • This is the another variant to sort array where we can specify start & end limits within char[] array

Method Signature:

public static void sort(char[] a, int fromIndex, int toIndex);

 

SortingPartialCharArray.java

package in.bench.resources.java.arrays.sorting;

import java.util.Arrays;

public class SortingPartialCharArray {

	public static void main(String[] args) {

		// sample char[] array
		char[] cArray = {'R', 'H', 'C', 'E', 'N', 'B', 'S'};

		// before sorting
		System.out.println("Before sorting : ");
		for(char c : cArray) {
			System.out.print(c + " ");
		}

		// sorting partial char[] array
		Arrays.sort(cArray, 1, 6);

		// after sorting
		System.out.println("\n\nAfter sorting : ");
		for(char c : cArray) {
			System.out.print(c + " ");
		}
	}
}

Output:

Before sorting : 
R H C E N B S 

After sorting : 
R B C E H N S 

 

Explanation:

  • Here, there are 7 char elements in char[] array
  • But, we have sorted char[] array starting from index-1 till index-5 leaving 1st & last element
  • Therefore, 1st and last element remains as it is after sorting and only middle elements are sorted

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/6/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

Happy Coding !!
Happy Learning !!