Arrays.sort()
Primitives
针对原始类型(primitive data types),Arrays.sort()用到了Quick Sort算法。
int[] numbers = {4, 9, 1, 3, 2, 8, 7, 0, 6, 5};
System.out.println(Arrays.toString(numbers));
java.util.Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
输出:
Before sorting: [4, 9, 1, 3, 2, 8, 7, 0, 6, 5]
After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Objects
针对对象类型(objects),Arrays.sort()用到了修改版的Merge Sort算法。
对象implements Comparable
public class Employee implements Comparable<Employee> {
private String name;
private int age;
private int salary;
public Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public int compareTo(Employee employee) {
return this.salary - employee.salary;
}
}
Some core classes in the JDK already implemented the Comparable
interface (e.g. String
, Date
and primitive wrappers: Integer, Long, Double
, etc), so that we can sort array of these types naturally.
String[] fruits = {"Orange", "Grape", "Apple", "Lemon", "Banana"};
System.out.println("Before sorting: " + Arrays.toString(fruits));
Arrays.sort(fruits);
System.out.println("After sorting: " + Arrays.toString(fruits));
输出为:
Before sorting: [Orange, Grape, Apple, Lemon, Banana]
After sorting: [Apple, Banana, Grape, Lemon, Orange]
对象implements Comparator
import java.util.Comparator;
public class EmployeeSalaryComparator implements Comparator<Employee> {
@Override
public int compare(Employee emp1, Employee emp2) {
return emp1.getSalary() - emp2.getSalary();
}
}
public class Employee {
private String name;
private int age;
private int salary;
public Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public static void main(String[] args) {
Employee[] newEmployees = new Employee[4];
newEmployees[0] = new Employee("Tom", 45, 80000);
newEmployees[1] = new Employee("Sam", 56, 75000);
newEmployees[2] = new Employee("Alex", 30, 120000);
newEmployees[3] = new Employee("Peter", 25, 60000);
System.out.println("Before sorting: " + Arrays.toString(newEmployees));
Arrays.sort(newEmployees, new EmployeeSalaryComparator());
System.out.println("After sorting: " + Arrays.toString(newEmployees));
}
}
使用Comparator
,就可以对Employee
对象进行不同方式的排序。
Collections.sort()
Collections.sort()
用法上面跟Arrays.sort()
基本差不多。它们两个的区别就在于,Arrays.sort()是用于array,Collections.sort()是用于list,且Arrays.sort()可以用于primitive data types。
Ascending Order
// Java program to demonstrate working of Collections.sort()
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Geeks For Geeks");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");
/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al);
// Let us print the sorted list
System.out.println("List after the use of Collections.sort():\n" + al);
}
}
Descending Order
// Java program to demonstrate working of Collections.sort()
// to descending order.
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Geeks For Geeks");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");
/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al, Collections.reverseOrder());
// Let us print the sorted list
System.out.println("List after the use of Collections.sort():\n" + al);
}
}