使用 Collections.sort() (最常用)
这是最传统、最简单的方法,适用于对 List 中的元素进行自然排序(升序)或使用自定义的 Comparator 进行排序。

对基本数据类型包装类的 ArrayList 排序
ArrayList 中存放的是实现了 Comparable 接口的对象(如 Integer, String, Double 等),可以直接使用 Collections.sort() 进行自然排序。
示例代码:
import java.util.ArrayList;
import java.util.Collections;
public class SortArrayListExample {
public static void main(String[] args) {
// 1. 创建一个存放 Integer 的 ArrayList
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(1);
numbers.add(8);
numbers.add(3);
numbers.add(2);
System.out.println("排序前: " + numbers);
// 2. 使用 Collections.sort() 进行排序 (默认是升序)
Collections.sort(numbers);
System.out.println("排序后 (升序): " + numbers);
}
}
输出:
排序前: [5, 1, 8, 3, 2]
排序后 (升序): [1, 2, 3, 5, 8]
对自定义对象的 ArrayList 排序
如果你有一个自定义的类,并且希望根据这个类的某个属性进行排序,你需要让这个类实现 Comparable 接口,或者在 Collections.sort() 方法中传入一个 Comparator。

让自定义类实现 Comparable 接口
Comparable 接口定义了对象的“自然排序”规则。
示例代码:
import java.util.ArrayList;
import java.util.Collections;
// 1. 定义一个学生类,并实现 Comparable 接口
class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{name='" + name + "', age=" + age + "}";
}
// 2. 实现 compareTo 方法,定义排序规则 (这里按年龄升序)
@Override
public int compareTo(Student other) {
return Integer.compare(this.age, other.age);
}
}
public class SortCustomObjectList {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));
students.add(new Student("David", 20));
System.out.println("排序前: " + students);
// 3. 使用 Collections.sort() 进行排序
// 因为 Student 类已经实现了 Comparable,所以可以直接排序
Collections.sort(students);
System.out.println("按年龄升序排序后: " + students);
}
}
输出:

排序前: [Student{name='Alice', age=22}, Student{name='Bob', age=20}, Student{name='Charlie', age=25}, Student{name='David', age=20}]
按年龄升序排序后: [Student{name='Bob', age=20}, Student{name='David', age=20}, Student{name='Alice', age=22}, Student{name='Charlie', age=25}]
使用 Comparator 进行灵活排序
Comparator 允许你在不修改原始类的情况下,定义多种不同的排序规则,它更灵活。
示例代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; // 需要导入 Comparator
// 使用上面定义的 Student 类
public class SortWithComparator {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));
students.add(new Student("David", 20));
System.out.println("排序前: " + students);
// 1. 按年龄升序排序
Collections.sort(students, Comparator.comparingInt(Student::getAge));
System.out.println("按年龄升序排序后: " + students);
// 2. 按年龄降序排序
Collections.sort(students, Comparator.comparingInt(Student::getAge).reversed());
System.out.println("按年龄降序排序后: " + students);
// 3. 按姓名字母顺序排序
Collections.sort(students, Comparator.comparing(Student::getName));
System.out.println("按姓名排序后: " + students);
}
}
输出:
排序前: [Student{name='Alice', age=22}, Student{name='Bob', age=20}, Student{name='Charlie', age=25}, Student{name='David', age=20}]
按年龄升序排序后: [Student{name='Bob', age=20}, Student{name='David', age=20}, Student{name='Alice', age=22}, Student{name='Charlie', age=25}]
按年龄降序排序后: [Student{name='Charlie', age=25}, Student{name='Alice', age=22}, Student{name='Bob', age=20}, Student{name='David', age=20}]
按姓名排序后: [Student{name='Alice', age=22}, Student{name='Bob', age=20}, Student{name='Charlie', age=25}, Student{name='David', age=20}]
使用 List.sort() (Java 8+ 推荐)
从 Java 8 开始,List 接口本身提供了一个 sort() 方法,它的功能和 Collections.sort() 完全一样,只是语法更面向对象一些。Collections.sort() 内部实际上也是调用了 list.sort(null)。
语法:
list.comparator
示例代码:
import java.util.ArrayList;
import java.util.Comparator;
public class ListSortExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(1);
numbers.add(8);
numbers.add(3);
System.out.println("排序前: " + numbers);
// 使用 List.sort() 进行升序排序
// null 表示使用自然排序
numbers.sort(null);
System.out.println("排序后 (升序): " + numbers);
// 使用自定义 Comparator 进行降序排序
numbers.sort(Comparator.reverseOrder());
System.out.println("再次排序后 (降序): " + numbers);
}
}
输出:
排序前: [5, 1, 8, 3]
排序后 (升序): [1, 3, 5, 8]
再次排序后 (降序): [8, 5, 3, 1]
使用 Java 8 Stream API (函数式风格)
这种方法适用于当你需要在排序后进行其他链式操作(如过滤、映射)时,或者你希望创建一个新的已排序列表而不修改原列表。
语法:
list.stream().sorted(Comparator).collect(Collectors.toList())
示例代码:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class StreamSortExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(1);
numbers.add(8);
numbers.add(3);
System.out.println("原始列表: " + numbers);
// 1. 创建一个新的已排序的列表 (原列表不变)
List<Integer> sortedNumbers = numbers.stream()
.sorted() // 自然排序
.collect(Collectors.toList());
System.out.println("排序后的新列表: " + sortedNumbers);
System.out.println("原始列表未改变: " + numbers);
// 2. 对自定义对象列表进行排序
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));
List<Student> sortedStudents = students.stream()
.sorted(Comparator.comparingInt(Student::getAge)) // 按年龄排序
.collect(Collectors.toList());
System.out.println("按年龄排序后的学生列表: " + sortedStudents);
}
}
输出:
原始列表: [5, 1, 8, 3]
排序后的新列表: [1, 3, 5, 8]
原始列表未改变: [5, 1, 8, 3]
按年龄排序后的学生列表: [Student{name='Bob', age=20}, Student{name='Alice', age=22}, Student{name='Charlie', age=25}]
总结与对比
| 方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
Collections.sort() |
简单直接,适用于所有 Java 版本。 对于自定义对象,通过 Comparable 或 Comparator 提供了清晰的排序逻辑。 |
会修改原始 List。语法相对老旧。 |
最通用的排序方法,尤其是在需要自定义 Comparable 规则时。 |
List.sort() |
语法更现代,面向对象。 功能与 Collections.sort() 完全相同。 |
需要 Java 8 或更高版本。 同样会修改原始 List。 |
Java 8+ 项目中的首选,代码更简洁。 |
Stream.sorted() |
不修改原始列表,返回一个新列表。 可以方便地进行链式操作(filter, map 等)。 语法非常符合函数式编程思想。 |
需要 Java 8 或更高版本。 对于简单排序,代码可能比前两者稍长。 如果处理非常大的数据集,可能会有额外的性能开销。 |
当你需要保持原列表不变,或者排序只是数据处理流程中的一步时,非常强大和灵活。 |
简单选择建议:
- 如果只是简单排序 (如
Integer,String列表),使用Collections.sort(list)或list.sort(null)都可以。 - 如果需要对自定义对象排序,并且排序规则是固定的,让类实现
Comparable接口。 - 如果排序规则多变,或者你不想修改原始类,请使用
Comparator,无论是通过Collections.sort(list, comparator)还是list.sort(comparator)。 - 如果你使用的是 Java 8+,并且希望代码更现代、函数式,或者不想修改原列表,
Stream.sorted()是最佳选择。
