[英]Sorting of ArrayList using Comparator in Java
I am having an issue with sorting an ArrayList with Ascending order.I am using Comparator and Collator Of Collection class .How can I achieve expected sorting order ? 我在按升序对ArrayList进行排序时遇到问题。我在使用Comparator和Collator of Collection类。如何实现预期的排序顺序? Help is much appreciated. 非常感谢您的帮助。
Ascending order calculated by Algorithm is : 通过算法计算的升序为:
[AutomationRejectNotification|,AutomationRejectNotification1011, AutomationRejectNotification1021,AutomationTestNotification1, AutomationTestNotification100,AutomationTestNotification2,testDisplay Template, Testing Chrome, Testing Field, Test Notfication, testnotif, Test Notification #1]
Expected Ascending Sorting order is: 预期的升序排序顺序为:
[AutomationRejectNotification1011, AutomationRejectNotification1021, AutomationRejectNotification|,AutomationTestNotification1, AutomationTestNotification2,AutomationTestNotification100,Test Notfication, Test Notification #1, testDisplay Template, Testing Chrome, Testing Field, testnotif]
Java code: 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);
Your Comparator
does not properly implement the contract of the Comparator
interface, so all bets are off. 您的Comparator
器未正确实施Comparator
界面的合同,因此所有押注均无效。 The compare()
method must have all of these properties: compare()
方法必须具有所有这些属性:
sgn(compare(x, y)) == -sgn(compare(y, x))
for all x and y.sgn(compare(x, y)) == -sgn(compare(y, x))
对于所有x和y。
((compare(x, y)>0) && (compare(y, z)>0))
impliescompare(x, z)>0
.((compare(x, y)>0) && (compare(y, z)>0))
意味着compare(x, z)>0
。
compare(x, y)==0
implies thatsgn(compare(x, z))==sgn(compare(y, z))
for all z.compare(x, y)==0
表示所有z的sgn(compare(x, z))==sgn(compare(y, z))
。
When both strings being compared contain a non-empty decimal digit sequence, you compare only by the numbers, whereas if at least one string is without any digits then you compare via the collator. 当两个比较的字符串都包含一个非空的十进制数字序列时,您仅按数字进行比较,而如果至少一个字符串没有任何数字,则可以通过整理程序进行比较。 That produces these results: 产生以下结果:
compare("z1", "y") > 0
compare("y", "x3") > 0
compare("z1", "x3") < 0
, which do not conform to the second required property (transitivity). ,它不符合第二个必需属性(传递性)。
Possibly what you want to do is is compare the longest leading digitless substrings as a first criterion, breaking any ties by comparing the trailing numbers, if any. 可能要做的是比较最长的前导无数字子字符串作为第一个条件,通过比较尾随数字(如果有的话)打破任何局限。 That might look like this: 可能看起来像这样:
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);
}
That would be consistent with the expected order you presented, but there are other ordering schemes that would also produce the same result for those particular elements. 这将与您提出的预期顺序一致,但是还有其他排序方案也可以为那些特定元素产生相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.