Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting names in a list alphabetically?

Can you please help me for sorting a list alphabetically

My code

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());
like image 296
prg Avatar asked Jul 26 '11 06:07

prg


1 Answers

You don't need to create a new comparator. Just call Collections.sort(emailList);.

UPDATE:

Collections.sort(emailList, new Comparator<String>()
{
    @Override
    public int compare(String text1, String text2)
    {
        return text1.compareToIgnoreCase(text2);
    }
});
like image 122
A. Abiri Avatar answered Oct 15 '22 15:10

A. Abiri