Sorting 1D And 2D Arrays In Java
Sorting arrays in Java is simple once you separate two ideas:
Arrays.sort(...)sorts the array in place.stream().sorted()creates a sorted stream; it does not mutate the original array.
For 2D arrays, you sort the outer array and provide a comparator for each inner row.
Sorting A 1D int[]
import java.util.Arrays;
class Example {
public static void main(String[] args) {
var a = new int[] { 1, 2, 4, 1, 7, 3, 8 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
Output:
[1, 1, 2, 3, 4, 7, 8]
Arrays.sort(a) changes a directly.
Stream Sorting
This line creates a sorted stream:
Arrays.stream(a).sorted();
But by itself, it does not store or print anything. Streams are lazy.
To get a sorted array:
int[] sorted = Arrays.stream(a)
.sorted()
.toArray();
To print sorted values:
Arrays.stream(a)
.sorted()
.forEach(System.out::println);
Use stream sorting when you want a new result and do not want to mutate the original array.
Sorting A 2D int[][]
A 2D array is an array of arrays.
var arr = new int[][] {
{ 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 0, 4 }
};
Access a value with row and column indexes:
System.out.println(arr[2][0]); // 3
Here:
arr[2]is{ 3, 4 }arr[2][0]is3
Sort 2D Array By First Column
import java.util.Arrays;
class Example {
public static void main(String[] args) {
var arr = new int[][] {
{ 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 0, 4 }
};
Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0]));
System.out.println(Arrays.deepToString(arr));
}
}
Output:
[[0, 4], [1, 2], [2, 3], [3, 4]]
The comparator compares the first value in each row:
(a, b) -> Integer.compare(a[0], b[0])
Use Integer.compare(...) instead of a[0] - b[0] because subtraction can overflow for large values.
Sort 2D Array By Second Column
Arrays.sort(arr, (a, b) -> Integer.compare(a[1], b[1]));
This sorts by column index 1.
Sort Descending
Sort by first column descending:
Arrays.sort(arr, (a, b) -> Integer.compare(b[0], a[0]));
Sort By First Column, Then Second Column
When first values tie, compare the second values:
Arrays.sort(arr, (a, b) -> {
int first = Integer.compare(a[0], b[0]);
if (first != 0) {
return first;
}
return Integer.compare(a[1], b[1]);
});
This is common in interval problems.
Example:
[[1, 3], [1, 2], [0, 4]]
After sorting by first column, then second column:
[[0, 4], [1, 2], [1, 3]]
Printing Arrays
For 1D arrays:
System.out.println(Arrays.toString(a));
For 2D arrays:
System.out.println(Arrays.deepToString(arr));
Without these helpers, Java prints the array object identity, not the useful contents.
Quick Decision Table
| Need | Use |
|---|---|
Sort int[] in place | Arrays.sort(a) |
Return sorted copy from int[] | Arrays.stream(a).sorted().toArray() |
Sort int[][] by first column | Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0])) |
Sort int[][] by second column | Arrays.sort(arr, (a, b) -> Integer.compare(a[1], b[1])) |
Print int[] | Arrays.toString(a) |
Print int[][] | Arrays.deepToString(arr) |
Takeaways
Arrays.sort(a)mutates a 1D array.Arrays.stream(a).sorted()is lazy and does not changea.- A 2D array is sorted by comparing rows.
- Use
Integer.compare(...)for comparator safety. - Use
Arrays.deepToString(...)to inspect 2D arrays.