私の質問は、カスタム オブジェクトを使用して ArrayList をそれらのプロパティの 1 つによって並べ替える方法ですが、カスタム条件から開始する方法についてです。
もっと詳しく説明しましょう。ここに私のコードがあります:
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;
}
});
}
これにより、ArrayList が大きいものから小さいものへと並べ替えられます。
しかし、私が望むのは、指定する開始番号から ArrayList をソートすることです。
たとえば、ArrayList に含まれている場合
5 3 9 1 14
数字を3から始めたいとしましょう。
3 5 9 14 1
十分に明確であることを願っています...
出来ますか?
@ヨアヒム・ザウアー
ありがとう、私はあなたのコードを少し編集し、戻り値を変更しましたが、うまくいきました!
編集されたコード:
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;
}
テスト例:
16777215
16448250
15790320
4013373
15790320 で並べ替え:
15790320
16448250
16777215
4013373