本文目录导读:

我来为您介绍Java数组排序的常见案例和实现方法:
基本数组排序
使用Arrays.sort()(最常用)
import java.util.Arrays;
public class ArraySortExample {
public static void main(String[] args) {
// 基本类型数组排序
int[] numbers = {5, 2, 8, 1, 9, 3};
Arrays.sort(numbers);
System.out.println("升序排序: " + Arrays.toString(numbers));
// 输出: [1, 2, 3, 5, 8, 9]
// 字符串数组排序
String[] names = {"Tom", "Alice", "Bob", "David"};
Arrays.sort(names);
System.out.println("字符串排序: " + Arrays.toString(names));
// 输出: [Alice, Bob, David, Tom]
}
}
降序排序
import java.util.Arrays;
import java.util.Collections;
public class DescendingSort {
public static void main(String[] args) {
// 方法1:使用包装类数组
Integer[] numbers = {5, 2, 8, 1, 9, 3};
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println("降序排序: " + Arrays.toString(numbers));
// 输出: [9, 8, 5, 3, 2, 1]
// 方法2:部分数组排序
int[] arr = {5, 2, 8, 1, 9, 3};
// 只排序索引1到4的元素(2, 8, 1, 9)
Arrays.sort(arr, 1, 5);
System.out.println("部分排序: " + Arrays.toString(arr));
// 输出: [5, 1, 2, 8, 9, 3]
}
}
自定义对象排序
import java.util.Arrays;
import java.util.Comparator;
class Student {
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return name + ":" + score;
}
}
public class CustomSort {
public static void main(String[] args) {
Student[] students = {
new Student("Tom", 85),
new Student("Alice", 92),
new Student("Bob", 78),
new Student("David", 95)
};
// 按成绩降序排序
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s2.score - s1.score; // 降序
}
});
System.out.println("按成绩排序: " + Arrays.toString(students));
// 输出: [David:95, Alice:92, Tom:85, Bob:78]
}
}
Lambda表达式简化(Java 8+)
import java.util.Arrays;
public class LambdaSort {
public static void main(String[] args) {
// 使用Lambda表达式
String[] names = {"Tom", "Alice", "Bob", "David"};
// 按字符串长度排序
Arrays.sort(names, (s1, s2) -> s1.length() - s2.length());
System.out.println("按长度排序: " + Arrays.toString(names));
// 输出: [Tom, Bob, Alice, David]
// 降序排列
Integer[] numbers = {5, 2, 8, 1, 9, 3};
Arrays.sort(numbers, (a, b) -> b - a);
System.out.println("Lambda降序: " + Arrays.toString(numbers));
}
}
使用流(Stream)排序
import java.util.Arrays;
import java.util.stream.Collectors;
public class StreamSort {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 9, 3};
// 使用Stream API排序
int[] sorted = Arrays.stream(numbers)
.sorted()
.toArray();
System.out.println("Stream排序: " + Arrays.toString(sorted));
// 降序排列
int[] descSorted = Arrays.stream(numbers)
.boxed()
.sorted((a, b) -> b - a)
.mapToInt(Integer::intValue)
.toArray();
System.out.println("Stream降序: " + Arrays.toString(descSorted));
}
}
多维数组排序
import java.util.Arrays;
import java.util.Comparator;
public class MultiDimSort {
public static void main(String[] args) {
int[][] matrix = {
{3, 1, 4},
{1, 5, 9},
{2, 6, 5}
};
// 按每行第一个元素排序
Arrays.sort(matrix, Comparator.comparingInt(row -> row[0]));
System.out.println("按每行首元素排序:");
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
}
性能比较示例
import java.util.Arrays;
import java.util.Random;
public class SortPerformance {
public static void main(String[] args) {
// 创建大数组测试性能
int size = 1000000;
int[] arr1 = new Random().ints(size, 0, 10000).toArray();
int[] arr2 = arr1.clone();
long startTime = System.currentTimeMillis();
Arrays.sort(arr1);
long endTime = System.currentTimeMillis();
System.out.println("Arrays.sort() 耗时: " + (endTime - startTime) + "ms");
// 并行排序(大数据推荐)
startTime = System.currentTimeMillis();
Arrays.parallelSort(arr2);
endTime = System.currentTimeMillis();
System.out.println("并行排序 耗时: " + (endTime - startTime) + "ms");
}
}
| 排序方法 | 使用场景 | 特点 |
|---|---|---|
| Arrays.sort() | 基本类型和对象 | 最简单,默认升序 |
| Comparator | 自定义排序 | 灵活,可降序 |
| Lambda | Java 8+ | 代码简洁 |
| Stream | 函数式编程 | 链式操作 |
| parallelSort | 大数据集 | 并行处理,更快 |
推荐使用:日常开发中最常用 Arrays.sort() 进行基本排序,需要自定义排序规则时使用 Comparator 或 Lambda 表达式。