根据拆分排序列表

时间:2013-04-04 10:32:24

标签: java android string sorting split

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

1 个答案:

答案 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);
    }
});