Arrays.sort()

Primitives

Arrays.sort() uses the Quick sort algorithm for primitive data types.

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));
Copy the code

Output:

Before sorting: [4.9.1.3.2.8.7.0.6.5]
After sorting: [0.1.2.3.4.5.6.7.8.9]
Copy the code

Objects

Arrays.sort() uses a modified version of Merge Sort for objects.

Object that 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; }}Copy the code

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));
Copy the code

The output is:

Before sorting: [Orange, Grape, Apple, Lemon, Banana]
After sorting: [Apple, Banana, Grape, Lemon, Orange]
Copy the code

Object implements the Comparator

import java.util.Comparator;

public class EmployeeSalaryComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee emp1, Employee emp2) {
        returnemp1.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)); }}Copy the code

Using the Comparator, you can sort Employee objects in different ways.

Collections.sort()

Collections.sort() is basically the same as arrays.sort (). Arrays.sort() is for array, collections.sort () is for list, and arrays.sort () is for 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); }}Copy the code

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); }}Copy the code