I need to achieve a functionality in java similar to sql sort where in i have a function where i pass a 2d array along with 2 column indexes, this function should return a sorted 2d array on the provided column indexes.
In short I need to sort the 2d array on 2 columns.
I was able to achieve the sort on 1 column wise , PFB the code for the same.
Arrays.sort(arr, new Comparator(){
private int col ;
public int compare(Object o1, Object o2) {
String[] s1 = (String[])o1;
String[] s2 = (String[])o2;
return s1[col].compareTo(s2[col]);
}
private Comparator init(int var){
col = var;
return this;
}
}.init(0)
);
But i need to enhance this functionality to sort based on 2 columns in 2d array. I request experts to help me out in this.
Example Input array
1 2 3 4 1 8 2 8 8 6 10 11 5 6 7 8 1 6 2 6Thanks in advance, RajuOutput : On sorting with Column 2,3 1 2 3 4 1 6 2 6 5 6 7 8 8 6 10 11 1 8 2 8