Java Arrays.sort() Example: Sort a Char Array with Output
Sorting a char array in Java is a one-liner. You call Arrays.sort() and the array is sorted in place. The code below does exactly that, then prints each character on its own line.
The code
import java.util.Arrays;
public class Hello {
public static void main(String[] args) {
char k[] = {'c', 'a', 's', 'r'};
Arrays.sort(k);
for (int i = 0; i < k.length; i++) {
System.out.println(k[i]);
}
}
}
What is happening here, line by line
- import java.util.Arrays; — Brings in the
Arraysutility class. It is in thejava.utilpackage. - char k[] = {'c', 'a', 's', 'r'}; — Declares and initializes a char array with four characters. The order is c, a, s, r.
- Arrays.sort(k); — Sorts the array in place. No new array is created. The original array
kis now sorted in ascending order. - for (int i = 0; i < k.length; i++) — Standard loop from index 0 to the last index.
- System.out.println(k[i]); — Prints each character on its own line.
Output
a c r s
The array started as {'c', 'a', 's', 'r'}. After sorting, it becomes {'a', 'c', 'r', 's'}. The loop prints each one, so you see a, c, r, s on separate lines.
Common mistakes and how to fix them
| Mistake | What happens | Fix |
|---|---|---|
Forgetting to import java.util.Arrays |
Compile error: "cannot find symbol Arrays" | Add import java.util.Arrays; at the top of the file. |
Calling Arrays.sort(k) but ignoring the result |
No error. But if you expected a return value, you get nothing. Arrays.sort() returns void. |
Remember: the array is sorted in place. Do not assign the result. |
Using char[] k = new char[4] without filling it |
Sorts the default '\u0000' characters. Output is invisible or blank. |
Always initialize the array with values before sorting. |
Loop condition i <= k.length |
ArrayIndexOutOfBoundsException |
Use i < k.length, not <=. |
Sorting in descending order
Arrays.sort() only sorts ascending for primitive types like char. To get descending order, sort ascending and then reverse the array manually:
Arrays.sort(k);
for (int i = 0, j = k.length - 1; i < j; i++, j--) {
char temp = k[i];
k[i] = k[j];
k[j] = temp;
}
After this, the array will be {'s', 'r', 'c', 'a'}, and the loop prints each character in descending order.
Where to go next
You now know how to sort a char array in Java, how Arrays.sort() works in place, and the mistakes that break the loop.
If you remember one thing, remember this: Arrays.sort() sorts the array in place. It returns nothing. You do not reassign the result.
If you want to go deeper, try these next steps:
- Sort an
int[]and aString[]with the same method. Notice that it works the same way for all primitive types andComparableobjects. - Sort a
String[]in reverse order usingArrays.sort(arr, Collections.reverseOrder()). This only works for object arrays, not primitive arrays. - Compare
Arrays.sort()withCollections.sort(). The first is for arrays, the second is for lists. - Explore the two-pointer trick used above for reversing. It is the same pattern used in many array problems in coding interviews.
No comments:
Post a Comment