Java Sort Array Objects using Comparable Interface

In this tutorial, we will see how to sort an array of primitives, strings and custom objects (employee) with a Comparable interface with an example.
  • Sorting an array of primitives – Arrays.sort() method
  • Sorting an array of Strings - Arrays.sort() method
  • Sorting an array of Employee objects - Arrays.sort() method
Learn Java Array at https://www.javaguides.net/2020/04/java-array-tutorial-for-beginners.html

1. Sorting an array of primitives – Arrays.sort() method

package com.java.array.tutorial.sorting;

import java.util.Arrays;

public class SortingArrayExamples {
    public static void main(String[] args) {
        // Sorting an array of primitives – Arrays.sort() method
        int[] myArray = {
            10,
            30,
            20,
            50,
            40
        };
        System.out.println("Before sorting => " + Arrays.toString(myArray));
        Arrays.sort(myArray);
        System.out.println("After sorting => " + Arrays.toString(myArray));
    }
}
Output:
Before sorting => [10, 30, 20, 50, 40]
After sorting => [10, 20, 30, 40, 50]

2. Sorting an array of Strings - Arrays.sort() method

package com.java.array.tutorial.sorting;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Sorting an array of Strings - Arrays.sort() method
 * @author Ramesh Fadatare
 *
 */
public class SortingArrayExamples {
    public static void main(String[] args) {
        // Sorting an array of Strings - Arrays.sort() method
        String[] strArray = {
            "B",
            "D",
            "A",
            "C",
            "E"
        };

        // ascending order
        System.out.println("Before sorting => " + Arrays.toString(strArray));
        Arrays.sort(strArray);
        System.out.println("After sorting => " + Arrays.toString(strArray));

        // descending order
        System.out.println("Before sorting => " + Arrays.toString(strArray));
        Arrays.sort(strArray, Comparator.reverseOrder());
        System.out.println("After sorting => " + Arrays.toString(strArray));
    }
}
Output:
Before sorting => [B, D, A, C, E]
After sorting => [A, B, C, D, E]
Before sorting => [A, B, C, D, E]
After sorting => [E, D, C, B, A]

3. Sorting an array of Employee objects - Arrays.sort() method

Suppose that we have an array of custom objects of type Employee:
package com.java.array.tutorial.sorting;

public class Employee implements Comparable < Employee > {
    private int id;
    private String name;
    private int salary;


    public Employee(int id, String name, int salary) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
    }
    @Override
    public int compareTo(Employee o) {
        return this.salary - o.salary;
    }
}
Now, we want to sort all employees in the array by their salary field in ascending order. To do so, it requires the Employee class to implement the Comparable interface and override the compareTo() method as follows:
package com.java.array.tutorial.sorting;

public class Employee implements Comparable < Employee > {
    // variables
    // constructor
    // getters and setters
 
    @Override
    public int compareTo(Employee o) {
       return this.salary - o.salary;
    }
}

Sort in Array of Employee objects in Ascending order

package com.java.array.tutorial.sorting;

import java.util.Arrays;

/**
 * Sorting an array of Employee objects - Arrays.sort() method
 * @author Ramesh Fadatare
 *
 */
public class SortingArrayExamples {
    public static void main(String[] args) {

        // Sorting an array of Employee objects - Arrays.sort() method
        Employee[] employees = {
            new Employee(10, "Ramesh", 50000),
            new Employee(20, "Tom", 60000),
            new Employee(30, "John", 80000),
            new Employee(40, "Tony", 30000)
        };

        // ascending order
        System.out.println("Before sorting => " + Arrays.toString(employees));
        Arrays.sort(employees); // ascending order
        System.out.println("After sorting => " + Arrays.toString(employees));
    }
}
Output:
Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=Tom, salary=60000], Employee [id=30, name=John, salary=80000], Employee [id=40, name=Tony, salary=30000]]
After sorting => [Employee [id=40, name=Tony, salary=30000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=Tom, salary=60000], Employee [id=30, name=John, salary=80000]]

Sort in Array of Employee objects in Descending order

In order to sort an array of employee objects in descending order, we need to change the below code:
package com.java.array.tutorial.sorting;

public class Employee implements Comparable < Employee > {
    // variables
    // constructor
    // getters and setters
 
    @Override
    public int compareTo(Employee o) {
        return o.salary - this.salary;
    }
}
Now let's test the code here:
package com.java.array.tutorial.sorting;

import java.util.Arrays;

/**
 * Sorting an array of Employee objects - Arrays.sort() method
 * @author Ramesh Fadatare
 *
 */
public class SortingArrayExamples {
    public static void main(String[] args) {

        // Sorting an array of Employee objects - Arrays.sort() method
        Employee[] employees = {
            new Employee(10, "Ramesh", 50000),
            new Employee(20, "Tom", 60000),
            new Employee(30, "John", 80000),
            new Employee(40, "Tony", 30000)
        };

        // descending order
        System.out.println("Before sorting => " + Arrays.toString(employees));
        Arrays.sort(employees); // descending order
        System.out.println("After sorting => " + Arrays.toString(employees));
    }
}
Output:
Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=Tom, salary=60000], Employee [id=30, name=John, salary=80000], Employee [id=40, name=Tony, salary=30000]]
After sorting => [Employee [id=30, name=John, salary=80000], Employee [id=20, name=Tom, salary=60000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=40, name=Tony, salary=30000]]
Learn Array at https://www.javaguides.net/2020/04/java-array-tutorial-for-beginners.html.

Comments