# 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
Sort 2d Array In Java | In this section, we will be explaining how to sort 2d arrays by using sorting techniques and functions available in the Java library.
2d Array Sorting In Java
Let us see 2d Array Sorting In Java using loops.
public class Main {
// sort 2D array in descending order
public static void sort2dArray(int arr[][]) {
int n = arr.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
for (int l = 0; l < n; l++) {
if (arr[i][j] > arr[k][l]) {
// swap
int temp = arr[i][j];
arr[i][j] = arr[k][l];
arr[k][l] = temp;
}
}
}
}
}
}
public static void display2dArray(int arr[][]) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String args[]) {
int array[][] =
{
{ 10, 28, 72, 91 },
{ 27, 33, 40, 52 },
{ 69, 75, 73, 72 },
{ 46, 33, 31, 2 }
};
System.out.println("2d Array: ");
display2dArray(array);
// sort the array
sort2dArray(array);
System.out.println("\nSorted 2d Array: ");
display2dArray(array);
}
}
Output:-
2d Array:
10 28 72 91
27 33 40 52
69 75 73 72
46 33 31 2
Sorted 2d Array:
91 75 73 72
72 69 52 46
40 33 33 31
28 27 10 2
In the above program the same original array is sorted. If you want to get a separate array (without changing the original array) then before sorting create a copy of 2D array and return the resultant array. We have discussed here in detail how to copy a 2D array in Java?
In the above example we have sorted array in descending order. But if you want to sort array in ascending order then you can use the below method instead.
// sort 2D array in ascending order
public static void sort2dArray(int arr[][]) {
int n = arr.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
for (int l = 0; l < n; l++) {
if (arr[i][j] < arr[k][l]) {
// swap
int temp = arr[i][j];
arr[i][j] = arr[k][l];
arr[k][l] = temp;
}
}
}
}
}
}
Output:-
2d Array:
10 28 72 91
27 33 40 52
69 75 73 72
46 33 31 2
Sorted 2d Array:
2 10 27 28
31 33 33 40
46 52 69 72
72 73 75 91
Sort 2d Array in Java Row Wise
In the above example we have sorted an entire 2d array. But sometimes we may need to sort them row wise. It means, in a particular row elements will be sorted in ascending or descending order.
// sort 2D array row wise in ascending order
public static void sort2dArrayRowWise(int arr[][]) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i].length - j - 1; k++) {
if (arr[i][k] > arr[i][k + 1]) {
// swap
int temp = arr[i][k];
arr[i][k] = arr[i][k + 1];
arr[i][k + 1] = temp;
}
}
}
}
}
Output:-
2d Array:
10 28 72 91
27 33 40 52
69 75 73 72
46 33 31 2
Sorted 2d Array:
10 28 72 91
27 33 40 52
69 72 73 75
2 31 33 46
To sort 2d array in Java row wise descending order change the if condition of the above method.
if (arr[i][k] < arr[i][k + 1]) {
// swap
int temp = arr[i][k];
arr[i][k] = arr[i][k + 1];
arr[i][k + 1] = temp;
}
Sort a 2d Array in Java Using sort() Method
In the Java Arrays class, a separate method is given to sort the one-dimesional array:- Arrays.sort() method. The Arrays.sort() method uses Dual-Pivot Quicksort technique to sort the array. We can take the help of Arrays.sort() method to sort a 2d array row wise.
2d array sorting in Java Row Wise using sort() Method
import java.util.Arrays;
public class Main {
public static void display2dArray(int arr[][]) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String args[]) {
int array[][] =
{
{ 10, 28, 72, 91 },
{ 27, 33, 40, 52 },
{ 69, 75, 73, 72 },
{ 46, 33, 31, 2 }
};
System.out.println("2d Array: ");
display2dArray(array);
// sort the array
for (int i = 0; i < array.length; i++) {
Arrays.sort(array[i]);
}
System.out.println("\nSorted 2d Array: ");
display2dArray(array);
}
}
Output:
2d Array:
10 28 72 91
27 33 40 52
69 75 73 72
46 33 31 2
Sorted 2d Array:
10 28 72 91
27 33 40 52
69 72 73 75
2 31 33 46
Java How To Sort A String 2d Array
We have seen several examples for sorting of integer 2d array. Now, let us see an example of sorting of string 2d array.
2d Array Sorting in Java Row Wise
import java.util.Arrays;
public class Main {
public static void display2dArray(String arr[][]) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String args[]) {
String array[][] =
{
{ "Stiphen", "John" },
{ "Henry", "Adam" },
{ "Potter", "Harry" }
};
System.out.println("2d Array: ");
display2dArray(array);
// sort the array
for (int i = 0; i < array.length; i++) {
Arrays.sort(array[i]);
}
System.out.println("\nSorted 2d Array: ");
display2dArray(array);
}
}
Output:
2d Array:
Stiphen John
Henry Adam
Potter Harry
Sorted 2d Array:
John Stiphen
Adam Henry
Harry Potter
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!