# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
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!