我将这些字符串放在一个 String 的 ArrayList 中,没有特定的顺序,但是当我调用 Collections.sort(listReference) 时,排序结果不正确,为什么 10 和 11(最后 2 个字符)出现在 07、08 之前, 09?
12880 20090506054200001
12880 20090506054200002
12880 20090513070200003
12880 20090513070200004
12880 20090520202600005
12880 20090520202600006
12880 20090520232900010
12880 20090520232900011
12880 20090520232900007
12880 20090520232900008
12880 20090520232900009
最佳答案
它对我来说很好用:
import java.util.*;
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("12880 20090506054200001");
list.add("12880 20090506054200002");
list.add("12880 20090513070200003");
list.add("12880 20090513070200004");
list.add("12880 20090520202600005");
list.add("12880 20090520202600006");
list.add("12880 20090520232900010");
list.add("12880 20090520232900011");
list.add("12880 20090520232900007");
list.add("12880 20090520232900008");
list.add("12880 20090520232900009");
Collections.sort(list);
for (String x : list) {
System.out.println(x);
}
}
}
输出:
12880 20090506054200001
12880 20090506054200002
12880 20090513070200003
12880 20090513070200004
12880 20090520202600005
12880 20090520202600006
12880 20090520232900007
12880 20090520232900008
12880 20090520232900009
12880 20090520232900010
12880 20090520232900011
您是否绝对确定您的 7/8/9 条目在其他地方没有“奇怪”的东西(例如,12880 和时间戳之间的不同空白元素)?
如果没有,您能否制作一个简短但完整的程序来演示问题?
关于java - Collections.sort 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/955710/