我有List
很长的Strings
:
test:test:test:test:test:test:test:ABC:test:test:test:test:test:test:
test:test:test:test:test:test:test:DEF:test:test:test:test:test:test:
test:test:test:test:test:test:test:GHI:test:test:test:test:test:test:
test:test:test:test:test:test:test:JKL:test:test:test:test:test:test:
test:test:test:test:test:test:test:MNO:test:test:test:test:test:test:
假设所有测试彼此不同。我想根据大写字母所在的特定位置对这些字符串进行排序。
for (int i = 0; i < aList.size(); i++) {
System.out.println(list.get(i).toString().split(":")[7]);
}
答案 0 :(得分:9)
使用Collections.sort()
并提供自定义Comparator
以强加您所需的订购条件。
这是一个例子(检查空值和遗漏的意外格式):
List<String> myList;
Collections.sort(myList, new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
String text1 = o1.split(":")[7];
String text2 = o2.split(":")[7];
return text1.compareTo(text2);
}
});