2

我有一个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]);
    }
4

1 回答 1

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);
    }
}); 
于 2013-04-04T10:35:17.603 回答