gpt4 book ai didi

java - Java中使用Comparator对ArrayList进行排序

转载 作者:行者123 更新时间:2023-11-30 02:19:11 25 4
gpt4 key购买 nike

我在按升序对 ArrayList 进行排序时遇到问题。我正在使用 Comparator 和 Collat​​or Of Collection 类。如何实现预期的排序顺序?非常感谢您的帮助。

算法计算出的升序为:

[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);

最佳答案

您的Comparator没有正确实现Comparator接口(interface)的约定,因此一切都失败了。 compare() 方法必须具有所有这些属性:

sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y.

((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

( API docs )

当要比较的两个字符串都包含非空十进制数字序列时,您通过数字进行比较,而如果至少一个字符串不包含任何数字,则您通过整理器进行比较。这会产生以下结果:

compare("z1", "y") > 0

compare("y", "x3") > 0

compare("z1", "x3") < 0

,不符合第二个必需属性(传递性)。

您可能想要做的是比较最长的前导无数字子字符串作为第一个标准,通过比较尾随数字(如果有)来打破任何联系。可能看起来像这样:

public static void listSort(List<String> o1, boolean order) {
final Pattern p = Pattern.compile("([^0-9]*)([0-9]+)?");
final Collator collator = Collator.getInstance(Locale.US);
Comparator<String> c = new Comparator<String>() {
public int compare(String object1, String object2) {
Matcher m1 = p.matcher(object1);
Matcher m2 = p.matcher(object2);

if (!m1.lookingAt() || !m2.lookingAt()) {
assert false : "Should never happen";
}

int result = collator.compare(m1.group(1), m2.group(1));

if (result == 0) {
String digits1 = m1.group(2);
String digits2 = m2.group(2);

if (digits1 != null && digits2 != null) {
Long number1 = Long.valueOf(digits1);
Long number2 = Long.valueOf(digits2);
result = number1.compareTo(number2);
} else if (digits1 != null) {
result = 1;
} else if (digits2 != null) {
result = -1;
}
}

return result;
}
};
o1.sort(c);
}

这将与您提供的预期顺序一致,但还有其他排序方案也会为这些特定元素产生相同的结果。

关于java - Java中使用Comparator对ArrayList进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47352801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com