In this article, we will discuss how to sort char[] array with example
1. 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
2. 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);
3. Sorting methods for char[] array:
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
4. Example for Sorting char[] Arrays
- Complete/Full Sorting of char[] Arrays
- Partial Sorting of char[] Arrays
4.1 Complete/Full Sorting of char[] Arrays
- 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
4.2 Partial Sorting of char[] Arrays
- 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.
Related Articles:
- Byte Arrays sorting
- char Arrays sorting
- short Arrays sorting
- Integer Arrays sorting
- Float Arrays sorting
- Double Arrays sorting
- Long Arrays sorting
- String Arrays sorting
- Java – How to Sort Arrays in Ascending and Descending order ?
- Java – String Arrays sorting in ascending & descending order
- Java – Sorting after merging two String[] Arrays
- Java – Sorting Arrays using Comparable and Comparator interface
- Java – How to Sort String[] arrays by its length in Ascending and Descending order ?
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 !!