If you have a 1D array containing objects of a custom class and you want to sort that array based on a specific property of the custom class, you can use the Arrays.sort
method along with a custom Comparator
. Here's an example:
Suppose you have a custom class called CustomObject
:
public class CustomObject implements Comparable<CustomObject> {
private int value;
public CustomObject(int value) {
this.value = value;
}
public int getValue() {
return value;
}
@Override
public int compareTo(CustomObject other) {
return Integer.compare(this.value, other.value);
}
@Override
public String toString() {
return "CustomObject{" +
"value=" + value +
'}';
}
}
Now, you can use this custom class in a 1D array and sort it:
import java.util.Arrays;
public class Sort1DArrayWithCustomClass {
public static void main(String[] args) {
// Example 1D array with CustomObject
CustomObject[] array = {
new CustomObject(3),
new CustomObject(1),
new CustomObject(6),
new CustomObject(0)
};
// Sort the 1D array based on the 'value' field in CustomObject
Arrays.sort(array);
// Display the sorted array
System.out.println(Arrays.toString(array));
}
}
In this example, the Arrays.sort
method is used directly on the 1D array of CustomObject
. The compareTo
method in the CustomObject
class is utilized for the comparison, which allows the array to be sorted based on the 'value' field of the custom class.
Using lamda expression
import java.util.Arrays;
public class CustomObjectSortingExample {
public static void main(String[] args) {
// Example array of custom objects
CustomObject[] array = {
new CustomObject(3),
new CustomObject(1),
new CustomObject(6),
new CustomObject(2),
new CustomObject(4)
};
// Sorting the array based on the 'value' property using lambda expression
Arrays.sort(array, (a, b) -> a.getValue() - b.getValue());
// Displaying the sorted array
System.out.println(Arrays.toString(array));
}
}
class CustomObject {
private int value;
public CustomObject(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String toString() {
return "CustomObject{" +
"value=" + value +
'}';
}
}
for sorting rows in higher dimensional arrays
import java.util.Arrays;
public class Sort2DArrayWithCustomClass {
public static void main(String[] args) {
// Example 2D array with CustomObject
CustomObject[][] matrix = {
{new CustomObject(3), new CustomObject(7), new CustomObject(2)},
{new CustomObject(1), new CustomObject(4), new CustomObject(5)},
{new CustomObject(6), new CustomObject(8), new CustomObject(9)},
{new CustomObject(0), new CustomObject(2), new CustomObject(4)}
};
// Sort the entire 2D array based on the 'value' field in CustomObject
Arrays.sort(matrix, (row1, row2) -> Integer.compare(row1[0].getValue(), row2[0].getValue()));
// Display the sorted matrix
for (CustomObject[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
}
In this example, the lambda expression (row1, row2) -> Integer.compare(row1[0].getValue(), row2[0].getValue())
is used as a comparator. It compares the 'value' field of the first element in each row to determine the sorting order. Adjust the index row[0]
based on the specific column you want to use for sorting.
Note: lamda functions do not work for arrays with primitive types
To work around primitive types comparator is used