I am trying to sort a 2d Arraylist based on the 1st element and when comparing if the two elements are the same then sort it by the 2nd element.
So my Arraylist looks like
[[1, 4, 0], [2, 5, 2], [3, 5, 3], [4, 3, 6]]
and I want it too look like this
[[4, 3, 6], [1, 4, 0], [2, 5, 2], [3, 5, 3]]
This is my ArrayList
ArrayList<ArrayList<Integer>> processes = new ArrayList<>();
This is what I have so far using selection sort
int smallInt;
int j;
int smallIntIndex;
for(int i = 1; i<=processes.size();i++){
smallInt = processes.get(i-1).get(1);
smallIntIndex = i-1;
for(j=i;j<processes.size();j++){
if(processes.get(j).get(1)==smallInt){
//not exactly sure what goes in here
}else if(processes.get(j).get(1)<smallInt){
smallInt = processes.get(j).get(1);
smallIntIndex = j;
}
}
ArrayList<Integer> temp = processes.get(smallIntIndex);
processes.set(smallIntIndex,processes.get(i-1));
processes.set(i-1,temp);
}
Are the labels (eg 1 or 2 or 3 - first / second / third entry) from a restricted set (eg only digits)? If so (as I think it is), you can sort them by applying Radix sort .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.