按字母顺序对列表中的名称进行排序?

时间:2011-07-26 06:24:50

标签: android list sorting

你能帮我按字母顺序排序一个列表吗

我的代码

emailList.add(contact.getUserName());                               
String[] emails = new String[emailList.size()];
emailList.toArray(emails);
namesList.add(name);

Collections.sort(emailList, new Comparator() {
    public int compare(Object o1, Object o2) {
        String name1 = (String) o1;
        String name2 = (String) o2;
        return name1.compareToIgnoreCase(name2);
    }
});

System.out.println("namesList.toString() = " + namesList.toString());

1 个答案:

答案 0 :(得分:27)

您无需创建新的比较器。只需致电Collections.sort(emailList);

<强>更新

Collections.sort(emailList, new Comparator<String>()
{
    @Override
    public int compare(String text1, String text2)
    {
        return text1.compareToIgnoreCase(text2);
    }
});