My question is about how to sort an ArrayList with custom Object by one of them properties but starting from a custom condition.
Let me explain better, here is my code:
public static void sortArrayListByProperty(ArrayList colorList){
Collections.sort(colorList, new Comparator(){
public int compare(Object emp1, Object emp2){
int intValue1 = ((ColorCounter)emp1).getIntColorValue();
int intValue2 = ((ColorCounter)emp2).getIntColorValue();
if(intValue1 < intValue2)
return 1;
else if(intValue1 > intValue2)
return -1;
else
return 0;
}
});
}
This will sort my ArrayList from bigger to smaller.
But what I want is to sort my ArrayList from a starting number that I will specify.
For example if ArrayList contains
5 3 9 1 14
lets say that I want numbers start from 3 then I need to have
3 5 9 14 1
I hope is clear enought…
Is it possible?
@Joachim Sauer
Thank you, I edited your code a little and changed return values and it worked!
Edited code:
if (cv1 >= threshold && cv2 < threshold) {
return -1;
} else if (cv2 >= threshold && cv2 < threshold) {
return -1;
} else if (cv1 < cv2) {
return 1;
} else if (cv1 > cv2) {
return 1;
} else {
return 0;
}
Test example:
16777215
16448250
15790320
4013373
Sorting by 15790320:
15790320
16448250
16777215
4013373
You could try this:
This is obviously very untested, might have some off-by-one-errors and might have flipped the -1/1 values. But it should show you the basic idea 😉