我该如何按字母顺序对列表进行排序?

224

我有一个包含国家名称的 List<String> 对象,如何按字母顺序对此列表进行排序?

16个回答

5
通过使用Collections.sort(),我们可以对列表进行排序。
public class EmployeeList {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<String> empNames= new ArrayList<String>();

        empNames.add("sudheer");
        empNames.add("kumar");
        empNames.add("surendra");
        empNames.add("kb");

        if(!empNames.isEmpty()){

            for(String emp:empNames){

                System.out.println(emp);
            }

            Collections.sort(empNames);

            System.out.println(empNames);
        }
    }
}

输出:

sudheer
kumar
surendra
kb
[kb, kumar, sudheer, surendra]

1
这并没有回答问题,因为String默认使用的排序方式是字典排序,而不是按字母顺序排序。 'Z'在字典排序中排在'a'之前,与按字母顺序排序不同。 - Chai T. Rex
@Chai Collections.sort() 只能按字典顺序对字符串进行排序。- 0...9 --> A-Z --> a-z - Shubham Arya

4

降序字母表:

List<String> list;
...
Collections.sort(list);
Collections.reverse(list);

这正是我在寻找的。首先,我需要将列表按照 A-Z 的顺序排序,然后按照 Z-A 进行比较。回答得好。 - japes Sophey

1
Java 8,
countries.sort((country1, country2) -> country1.compareTo(country2));

如果 String's compareTo 不适合您的需求,您可以提供任何其他比较器。

0
public static void sortByAlphabetCountry(List<Employee> listCountry) {
    listCountry.sort((o1, o2) -> {
        return o1.getName().compareTo(o2.getName());
    });
}

1
你的回答对已经提供的答案有什么补充? - Moritz Ringler

0

在JAVA 8中同样适用:

//Assecnding order
listOfCountryNames.stream().sorted().forEach((x) -> System.out.println(x));

//Decending order
listOfCountryNames.stream().sorted((o1, o2) -> o2.compareTo(o1)).forEach((x) -> System.out.println(x));

1
这并没有回答问题,因为用于String的默认排序是字典排序,而不是按字母顺序排序。 'Z'在字典排序中排在'a'之前,与按字母顺序排序不同。 - Chai T. Rex

-1
//Here is sorted List alphabetically with syncronized
package com.mnas.technology.automation.utility;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
/**
* 
* @author manoj.kumar
*/
public class SynchronizedArrayList {
static Logger log = Logger.getLogger(SynchronizedArrayList.class.getName());
@SuppressWarnings("unchecked")
public static void main(String[] args) {

List<Employee> synchronizedList = Collections.synchronizedList(new ArrayList<Employee>());
synchronizedList.add(new Employee("Aditya"));
synchronizedList.add(new Employee("Siddharth"));
synchronizedList.add(new Employee("Manoj"));
Collections.sort(synchronizedList, new Comparator() {
public int compare(Object synchronizedListOne, Object synchronizedListTwo) {
//use instanceof to verify the references are indeed of the type in question
return ((Employee)synchronizedListOne).name
.compareTo(((Employee)synchronizedListTwo).name);
}
}); 
/*for( Employee sd : synchronizedList) {
log.info("Sorted Synchronized Array List..."+sd.name);
}*/

// when iterating over a synchronized list, we need to synchronize access to the synchronized list
synchronized (synchronizedList) {
Iterator<Employee> iterator = synchronizedList.iterator();
while (iterator.hasNext()) {
log.info("Sorted Synchronized Array List Items: " + iterator.next().name);
}
}

}
}
class Employee {
String name;
Employee (String name) {
this.name = name;

}
}

但是为什么呢?为什么使用这50行代码比Lena答案中的1行代码更好呢? - james.garriss
因为,sort(List<T>, Comparator<? super T>) 方法用于根据指定比较器引起的顺序对指定列表进行排序。当您想要按照员工的姓名和年龄对员工列表进行排序时,请使用 Collections sort(List, Comparator) 方法。 - user4423251
抱歉,这里感受不到任何爱意。您回答的是另一个问题,即如何按属性对对象列表进行排序。您的代码可能对此有用,但对于OP的问题来说,它显得过于复杂了。 :-( - james.garriss
这个回答并没有解决问题,因为用于 String 的默认排序是按字典顺序而非按字母表顺序。'Z' 在字典排序中排在 'a' 之前,与字母表排序不同。 - Chai T. Rex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接