ArrayListを昇順でソートする際に問題が発生しています。ComparatorとCollator Of Collectionクラスを使用しています。ヘルプは非常に感謝しています。アルゴリズムによって計算Javaのコンパレータを使用したArrayListの並べ替え
昇順順序は次のとおりです。
[AutomationRejectNotification|,AutomationRejectNotification1011, AutomationRejectNotification1021,AutomationTestNotification1, AutomationTestNotification100,AutomationTestNotification2,testDisplay Template, Testing Chrome, Testing Field, Test Notfication, testnotif, Test Notification #1]
期待昇順ソート順序は次のとおりです。
[AutomationRejectNotification1011, AutomationRejectNotification1021, AutomationRejectNotification|,AutomationTestNotification1, AutomationTestNotification2,AutomationTestNotification100,Test Notfication, Test Notification #1, testDisplay Template, Testing Chrome, Testing Field, testnotif]
Javaコード:
public static void listSort(List<String> o1, boolean order) {
final Pattern p = Pattern.compile("^\\d+");
Comparator<String> c = new Comparator<String>() {
public int compare(String object1, String object2) {
Collator collator = Collator.getInstance(Locale.US);
Matcher m = p.matcher(object1);
if (!m.find()) {
return collator.compare(object1, object2);
} else {
Long number2 = null;
Long number1 = Long.parseLong(m.group());
m = p.matcher(object2);
if (!m.find()) {
return collator.compare(object1, object2);
} else {
number2 = Long.parseLong(m.group());
int comparison = number1.compareTo(number2);
if (comparison != 0) {
return comparison;
} else {
return collator.compare(object1, object2);
}
}
}
}
};
o1.sort(c);
これはCollatorが動作する方法です。この記事を見てください:https://documentation.progress.com/output/Corticon/5.3.2/suite_prototype/rfi1341263753418.html – WinterN
いいえ、@WinterN、それはCollatorの仕組みではありません。特に、米国ロケールでは、 '|'文字は10進数字の後ろに来るので、 '' AutomationRejectNotification | ""はOPで報告された順序に反して、 "" AutomationRejectNotification1011 "'の後に照合することに注意してください。 –
@kaps - 予想される出力の例を1つ提供するのではなく、任意の入力リストに適用されるソート規則を指定できますか? –