Sorting Arrays in Java – 在Java中对数组进行排序

最后修改: 2018年 12月 20日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll discuss common methods to sort arrays in ascending and descending order.

在本教程中,我们将讨论按升序和降序对数组进行排序的常用方法。

We’ll look at using Java’s Arrays class sorting method as well as implementing our own Comparator to order our arrays’ values.

我们将研究如何使用Java的Arrays类的排序方法,以及实现我们自己的Comparator来排列数组的值。

2. Object Definitions

2.对象的定义

Before we begin, let’s quickly define a few arrays that we’ll sort throughout this tutorial. First, we’ll create an array of ints and an array of strings:

在我们开始之前,让我们快速定义几个数组,我们将在本教程中对其进行分类。首先,我们将创建一个ints数组和一个字符串数组。

int[] numbers = new int[] { -8, 7, 5, 9, 10, -2, 3 };
String[] strings = new String[] { "learning", "java", "with", "baeldung" };

And let’s also create an array of Employee objects where each employee has an id and a name attribute:

让我们也创建一个雇员对象的数组,每个雇员都有一个id和一个name属性。

Employee john = new Employee(6, "John");
Employee mary = new Employee(3, "Mary");
Employee david = new Employee(4, "David");
Employee[] employees = new Employee[] { john, mary, david };

3. Sorting in Ascending Order

3.按升序排序

Java’s util.Arrays.sort method provides us with a quick and simple way to sort an array of primitives or objects that implement the Comparable interface in ascending order.

Java的util.Arrays.sort方法为我们提供了一种快速而简单的方法来对实现Comparable接口的基元或对象阵列按升序排序。

When sorting primitives, the Arrays.sort method uses a Dual-Pivot implementation of Quicksort. However, when sorting objects an iterative implementation of MergeSort is used.

当对基元进行排序时,Arrays.sort 方法使用Quicksort的双枢实现。然而,当对对象进行排序时,会使用MergeSort的迭代实现。

3.1. Primitives

3.1.基本原理

To sort a primitive array in ascending order, we pass our array to the sort method:

为了对一个原始数组进行升序排序,我们将数组传递给sort方法。

Arrays.sort(numbers);
assertArrayEquals(new int[] { -8, -2, 3, 5, 7, 9, 10 }, numbers);

3.2. Objects That Implement Comparable

3.2.实现可比较的对象

For objects that implement the Comparable interface, as with our primitive array, we can also simply pass our array to the sort method:

对于实现Comparable接口的对象,就像我们的原始数组一样,我们也可以简单地将我们的数组传递给sort方法。

Arrays.sort(strings);
assertArrayEquals(new String[] { "baeldung", "java", "learning", "with" }, strings);

3.3. Objects That Don’t Implement Comparable

3.3.不实现可比较的对象

Sorting objects that don’t implement the Comparable Interface, like our array of Employees, requires us to specify our own comparator.

对没有实现Comparable接口的对象进行排序,比如我们的Employees数组,需要我们指定自己的比较器。

We can do this very easily in Java 8 by specifying the property that we would like to compare our Employee objects on within our Comparator:

在Java 8中,我们可以通过指定我们希望在Comparator中比较我们的Employee 对象的属性来非常容易地做到这一点。

Arrays.sort(employees, Comparator.comparing(Employee::getName));
assertArrayEquals(new Employee[] { david, john, mary }, employees);

In this case, we’ve specified that we would like to order our employees by their name attributes.

在这种情况下,我们已经指定要按照员工的姓名属性来排序。

We can also sort our objects on more that one attribute by chaining our comparisons using Comparator’s thenComparing method:

我们还可以通过使用Comparator的thenComparing方法将我们的对象按多个属性进行排序:

Arrays.sort(employees, Comparator.comparing(Employee::getName).thenComparing(Employee::getId));

4. Sorting in Descending Order

4.按降序排序

4.1. Primitives

4.1.基本原理

Sorting a primitive array in descending order is not quite as simple as sorting it in ascending order because Java doesn’t support the use of Comparators on primitive types. To overcome this shortfall we have a few options.

对原始数组进行降序排序并不像升序排序那么简单,因为Java不支持在原始类型上使用Comparators。为了克服这一缺陷,我们有几个选择。

First, we could sort our array in ascending order and then do an in-place reversal of the array.

首先,我们可以对我们的数组进行升序排序,然后对数组进行原地反转

Second, could convert our array to a list, use Guava’s Lists.reverse() method and then convert our list back into an array.

其次,可以将我们的数组转换为列表,使用Guava的Lists.reverse()方法,然后将我们的列表转回为数组。

Finally, we could transform our array to a Stream and then map it back to an int array. It has a nice advantage of being a one-liner and just using core Java:

最后,我们可以将我们的数组转换为Stream,然后再将其映射为int数组。它有一个很好的优势,那就是一个单行本,而且只需使用核心Java:

numbers = IntStream.of(numbers).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray();
assertArrayEquals(new int[] { 10, 9, 7, 5, 3, -2, -8 }, numbers);

The reason this works is that boxed turns each int into an Integer, which does implement Comparator.

这样做的原因是,boxed将每个int变成Integerdoes实现Comparator。

4.2. Objects That Implement Comparable

4.2.实现可比较的对象

Sorting an object array that implements the Comparable interface in descending order is quite simple. All we need to do is pass a Comparator as the second parameter of our sort method.

对实现了Comparable接口的对象数组进行降序排序是非常简单的。我们需要做的就是传递一个Comparator作为我们sort方法的第二个参数。

In Java 8 we can use Comparator.reverseOrder() to indicate that we would like our array to be sorted in descending order:

在Java 8中,我们可以使用Comparator.reverseOrder()来表示我们希望我们的数组以降序排序:

Arrays.sort(strings, Comparator.reverseOrder());
assertArrayEquals(new String[] { "with", "learning", "java", "baeldung" }, strings);

4.3. Objects That Don’t Implement Comparable

4.3.不实现可比较的对象

Similarly to sorting objects that implement comparable, we can reverse the order of our custom Comparator by adding reversed() at the end of our comparison definition:

与实现可比较的对象的排序类似,我们可以通过在比较定义的末尾添加reversed() 来扭转我们的自定义Comparator 的顺序。

Arrays.sort(employees, Comparator.comparing(Employee::getName).reversed());
assertArrayEquals(new Employee[] { mary, john, david }, employees);

5. Conclusion

5.结论

In this article, we discussed how to sort arrays of primitives and objects in ascending and descending order using the Arrays.sort method.

在这篇文章中,我们讨论了如何使用Arrays.sort方法对基元和对象的数组进行升序和降序排序。

As usual, the source code from this article can be found over on Github.

像往常一样,本文的源代码可以在Github上找到over