在Java开发过程中,数组和集合的排序是高频操作,Java标准库提供了Arrays和Collections两个工具类,分别针对数组和集合对象封装了成熟的排序方法,开发者可以直接调用实现高效的排序功能。

Arrays工具类的排序用法
Arrays类位于java.util包下,提供了大量操作数组的静态方法,其中sort方法支持多种类型的数组排序,包括基本类型数组和对象类型数组。
基本类型数组排序
对于int、double、char等基本类型的数组,Arrays.sort会按照默认的自然顺序进行升序排列,无需额外传入比较规则。
import java.util.Arrays;
public class ArraySortDemo {
public static void main(String[] args) {
// 定义int类型数组
int[] intArray = {5, 2, 8, 1, 9};
// 调用Arrays.sort进行升序排序
Arrays.sort(intArray);
// 输出排序后的数组
System.out.println(Arrays.toString(intArray));
// 定义double类型数组
double[] doubleArray = {3.14, 1.59, 2.65, 0.99};
Arrays.sort(doubleArray);
System.out.println(Arrays.toString(doubleArray));
}
}
对象类型数组排序
如果要对对象数组排序,对象需要实现Comparable接口,重写compareTo方法定义排序规则,或者调用重载的sort方法传入Comparator比较器。
import java.util.Arrays;
import java.util.Comparator;
class Student implements Comparable<Student> {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
// 实现Comparable接口,按分数升序排序
@Override
public int compareTo(Student o) {
return this.score - o.score;
}
@Override
public String toString() {
return "Student{name='" + name + "', score=" + score + "}";
}
}
public class ObjectArraySortDemo {
public static void main(String[] args) {
Student[] students = {
new Student("张三", 85),
new Student("李四", 92),
new Student("王五", 78)
};
// 按Comparable定义的规则排序
Arrays.sort(students);
System.out.println(Arrays.toString(students));
// 传入Comparator按名字长度排序
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.name.length() - o2.name.length();
}
});
System.out.println(Arrays.toString(students));
}
}
Collections工具类的排序用法
Collections类同样位于java.util包下,提供了操作集合的静态方法,其sort方法专门用于List接口的实现类排序,不支持Set、Map等无序集合的排序。
默认排序
和Arrays类似,如果List中的元素实现了Comparable接口,调用Collections.sort会按照自然顺序升序排列。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListSortDemo {
public static void main(String[] args) {
List<Integer> numList = new ArrayList<>();
numList.add(10);
numList.add(3);
numList.add(7);
numList.add(1);
// 默认升序排序
Collections.sort(numList);
System.out.println(numList);
List<String> strList = new ArrayList<>();
strList.add("banana");
strList.add("apple");
strList.add("cherry");
Collections.sort(strList);
System.out.println(strList);
}
}
自定义排序规则
如果需要自定义排序逻辑,可以传入Comparator比较器,实现复杂的排序需求。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public String toString() {
return "Book{title='" + title + "', price=" + price + "}";
}
}
public class CustomListSortDemo {
public static void main(String[] args) {
List<Book> bookList = new ArrayList<>();
bookList.add(new Book("Java基础", 59.9));
bookList.add(new Book("Spring实战", 89.9));
bookList.add(new Book("算法导论", 79.9));
// 按价格降序排序
Collections.sort(bookList, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
// 降序用o2减o1,升序用o1减o2
return Double.compare(o2.price, o1.price);
}
});
System.out.println(bookList);
}
}
两者的差异与注意事项
- 适用对象不同:Arrays.sort用于数组排序,支持基本类型和对象类型数组;Collections.sort仅用于List集合排序,不支持数组和其他集合类型。
- 排序算法不同:Arrays.sort对于基本类型数组采用双轴快速排序,对于对象数组采用TimSort;Collections.sort底层调用的是List的sort方法,默认也是TimSort实现。
- 排序稳定性:两者的对象排序都是稳定的,即相等元素的相对顺序在排序后不会改变。
- 自定义规则时,基本类型数组无法直接传入Comparator,需要先转换为对应的包装类数组,再使用Arrays.sort的重载方法。
在使用这两个工具类排序时,要注意如果集合或数组中包含null元素,且没有自定义处理null的比较规则,会抛出NullPointerException,需要提前做好空值校验或者比较器中处理null的情况。
Java排序Arrays工具类Collections工具类数组排序集合排序修改时间:2026-06-30 21:42:32