Ich habe ein Problem mit dem Sortieren einer ArrayList mit aufsteigender Reihenfolge. Ich verwende Comparator und Collator Of Collection-Klasse. Wie kann ich die erwartete Sortierreihenfolge erreichen? Hilfe wird sehr geschätzt.Sortierung von ArrayList mit Comparator in Java
aufsteigende Sortierung durch den Algorithmus berechnet wird:
[AutomationRejectNotification|,AutomationRejectNotification1011, AutomationRejectNotification1021,AutomationTestNotification1, AutomationTestNotification100,AutomationTestNotification2,testDisplay Template, Testing Chrome, Testing Field, Test Notfication, testnotif, Test Notification #1]
Erwartete aufsteigende Sortierung Reihenfolge ist:
[AutomationRejectNotification1011, AutomationRejectNotification1021, AutomationRejectNotification|,AutomationTestNotification1, AutomationTestNotification2,AutomationTestNotification100,Test Notfication, Test Notification #1, testDisplay Template, Testing Chrome, Testing Field, testnotif]
Java-Code:
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);
So funktioniert Collator. Werfen Sie einen Blick auf diesen Artikel: https://documentation.progress.com/output/Corticon/5.3.2/suite_prototype/rfi1341263753418.html – WinterN
Nein, @WinterN, es ist * nicht * wie 'Collator' funktioniert. Beachten Sie insbesondere, dass im US-Gebietsschema das '|' -Zeichen * nach * den Dezimalziffern steht, so dass "AutomationRejectNotification |" nach "AutomationRejectNotification1011" 'kollationiert, im Gegensatz zu der vom OP gemeldeten Reihenfolge. –
@Kaps - können Sie anstelle eines einzigen Beispiels für Ihre erwartete Ausgabe die Sortierregel angeben, die für eine beliebige Eingabeliste gelten würde? –