In this article, we will discuss how to sort Arrays in both ascending and descending order of Integer, String and Employee objects
Sorting Arrays :
- Sorting Arrays in Ascending-order
- Sorting Arrays in Descending-order
1. Sorting Arrays in Ascending order :
- Sorting Integer[] Arrays in Ascending-order
- Sorting String[] Arrays in Ascending-order
- Sorting Employee[] Arrays in Ascending-order
1.1 Sorting Integer[] Arrays in Ascending order :
- In Java 8, Arrays.sort() method accepts 2 input-arguments where,
- 1st argument is the original/actual arrays to be sorted
- 2nd argument is the Comparator object for natural order sorting using Comparator.naturalOrder();
AscendingOrderSortingOfIntegerArraysInJava8.java
package in.bench.resources.arrays.sorting;
import java.util.Arrays;
import java.util.Comparator;
public class AscendingOrderSortingOfIntegerArraysInJava8 {
public static void main(String[] args) {
// sample Integer[] array
Integer[] intArray = {
1975,
2003,
1979,
1992,
1983,
1999,
1987
};
// before sorting
System.out.println("Before sorting - Integer[] Arrays\n");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
// sorting Integer[] arrays in ascending order
Arrays.sort(intArray, Comparator.naturalOrder());
// after sorting
System.out.println("\nAfter sorting in Ascending-order - Integer[] Arrays\n");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
}
}
Output:
Before sorting - Integer[] Arrays
1975
2003
1979
1992
1983
1999
1987
After sorting in Ascending-order - Integer[] Arrays
1975
1979
1983
1987
1992
1999
2003
1.2 Sorting String[] Arrays in Ascending order :
- In Java 8, Arrays.sort() method accepts 2 input-arguments where,
- 1st argument is the original/actual arrays to be sorted
- 2nd argument is the Comparator object for natural order sorting using Comparator.naturalOrder();
AscendingOrderSortingOfStringArraysInJava8.java
package in.bench.resources.arrays.sorting;
import java.util.Arrays;
import java.util.Comparator;
public class AscendingOrderSortingOfStringArraysInJava8 {
public static void main(String[] args) {
// sample String[] array
String[] strArray = {
"Clive",
"Kapil",
"Allan",
"Imran",
"Arjuna",
"Steve",
"Ricky"
};
// before sorting
System.out.println("Before sorting - String[] Arrays\n");
for(String str : strArray) {
System.out.println(str);
}
// sorting String[] arrays in ascending order
Arrays.sort(strArray, Comparator.naturalOrder());
// after sorting
System.out.println("\nAfter sorting in Ascending-order - String[] Arrays\n");
for(String str : strArray) {
System.out.println(str);
}
}
}
Output:
Before sorting - String[] Arrays
Clive
Kapil
Allan
Imran
Arjuna
Steve
Ricky
After sorting in Ascending-order - String[] Arrays
Allan
Arjuna
Clive
Imran
Kapil
Ricky
Steve
1.3 Sorting Employee[] Arrays in Ascending order :
- In Java 8, Arrays.sort() method accepts 2 input-arguments where,
- 1st argument is the original/actual arrays to be sorted
- 2nd argument is the Comparator object for natural order sorting of first name using method reference as shown in the below example
- Employee class consists of 4 member variables with
- 4-argument parameterized constructor
- setters and getters
- overriding toString() method
- removed for brevity
Employee.java
package in.bench.resources.arrays.sorting;
public class Employee {
// member variables
private int id;
private String firstName;
private String lastName;
private float salary;
// 4-argument parameterized constructor
// getters & setters
// toString() method
}
AscendingOrderSortingOfEmployeeArraysInJava8.java
package in.bench.resources.arrays.sorting;
import java.util.Arrays;
import java.util.Comparator;
public class AscendingOrderSortingOfEmployeeArraysInJava8 {
public static void main(String[] args) {
// sample Employee[] array
Employee[] employees = new Employee[]{
new Employee(1001, "Suresh", "Krishna", 25000.00f),
new Employee(1002, "Mani", "Ratnam", 58000.00f),
new Employee(1003, "Balu", "Mahendra", 46000.00f),
new Employee(1004, "Venkat", "Prabhu", 35000.00f),
new Employee(1005, "Lokesh", "Kanagaraj", 75000.00f)
};
// before sorting
System.out.println("Before sorting - Employee[] Arrays\n");
for(Employee emp : employees) {
System.out.println(emp);
}
// sorting Employee[] arrays in ascending order
Arrays.sort(employees, Comparator.comparing(Employee::getFirstName));
// after sorting
System.out.println("\nAfter sorting in Ascending-order of first name"
+ " - Employee[] Arrays\n");
for(Employee emp : employees) {
System.out.println(emp);
}
}
}
Output:
Before sorting - Employee[] Arrays
Employee [id=1001, firstName=Suresh, lastName=Krishna, salary=25000.0]
Employee [id=1002, firstName=Mani, lastName=Ratnam, salary=58000.0]
Employee [id=1003, firstName=Balu, lastName=Mahendra, salary=46000.0]
Employee [id=1004, firstName=Venkat, lastName=Prabhu, salary=35000.0]
Employee [id=1005, firstName=Lokesh, lastName=Kanagaraj, salary=75000.0]
After sorting in Ascending-order of first name - Employee[] Arrays
Employee [id=1003, firstName=Balu, lastName=Mahendra, salary=46000.0]
Employee [id=1005, firstName=Lokesh, lastName=Kanagaraj, salary=75000.0]
Employee [id=1002, firstName=Mani, lastName=Ratnam, salary=58000.0]
Employee [id=1001, firstName=Suresh, lastName=Krishna, salary=25000.0]
Employee [id=1004, firstName=Venkat, lastName=Prabhu, salary=35000.0]
2. Sorting Arrays in Descending order :
- Sorting Integer[] Arrays in Descending-order
- Sorting String[] Arrays in Descending-order
- Sorting Employee[] Arrays in Descending-order
2.1 Sorting Integer[] Arrays in Descending order :
- In Java 8, Arrays.sort() method accepts 2 input-arguments where,
- 1st argument is the original/actual arrays to be sorted
- 2nd argument is the Comparator object for reverse order sorting using Comparator.reverseOrder();
DescendingOrderSortingOfIntegerArraysInJava8.java
package in.bench.resources.arrays.sorting;
import java.util.Arrays;
import java.util.Comparator;
public class DescendingOrderSortingOfIntegerArraysInJava8 {
public static void main(String[] args) {
// sample Integer[] array
Integer[] intArray = {
1975,
2003,
1979,
1992,
1983,
1999,
1987
};
// before sorting
System.out.println("Before sorting - Integer[] Arrays\n");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
// sorting Integer[] arrays in descending order
Arrays.sort(intArray, Comparator.reverseOrder());
// after sorting
System.out.println("\nAfter sorting in Descending-order - Integer[] Arrays\n");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
}
}
Output:
Before sorting - Integer[] Arrays
1975
2003
1979
1992
1983
1999
1987
After sorting in Descending-order - Integer[] Arrays
2003
1999
1992
1987
1983
1979
1975
2.2 Sorting String[] Arrays in Descending order :
- In Java 8, Arrays.sort() method accepts 2 input-arguments where,
- 1st argument is the original/actual arrays to be sorted
- 2nd argument is the Comparator object for reverse order sorting using Comparator.reverseOrder();
DescendingOrderSortingOfStringArraysInJava8.java
package in.bench.resources.arrays.sorting;
import java.util.Arrays;
import java.util.Comparator;
public class DescendingOrderSortingOfStringArraysInJava8 {
public static void main(String[] args) {
// sample String[] array
String[] strArray = {
"Clive",
"Kapil",
"Allan",
"Imran",
"Arjuna",
"Steve",
"Ricky"
};
// before sorting
System.out.println("Before sorting - String[] Arrays\n");
for(String str : strArray) {
System.out.println(str);
}
// sorting String[] arrays in descending order
Arrays.sort(strArray, Comparator.reverseOrder());
// after sorting
System.out.println("\nAfter sorting in Descending-order - String[] Arrays\n");
for(String str : strArray) {
System.out.println(str);
}
}
}
Output:
Before sorting - String[] Arrays
Clive
Kapil
Allan
Imran
Arjuna
Steve
Ricky
After sorting in Descending-order - String[] Arrays
Steve
Ricky
Kapil
Imran
Clive
Arjuna
Allan
2.3 Sorting Employee[] Arrays in Descending order :
- In Java 8, Arrays.sort() method accepts 2 input-arguments where,
- 1st argument is the original/actual arrays to be sorted
- 2nd argument is the Comparator object for reverse order sorting of first name using method reference as shown in the below example
- Employee class consists of 4 member variables with
- 4-argument parameterized constructor
- setters and getters
- overriding toString() method
- removed for brevity
Employee.java
package in.bench.resources.arrays.sorting;
public class Employee {
// member variables
private int id;
private String firstName;
private String lastName;
private float salary;
// 4-argument parameterized constructor
// getters & setters
// toString() method
}
DescendingOrderSortingOfEmployeeArraysInJava8.java
package in.bench.resources.arrays.sorting;
import java.util.Arrays;
import java.util.Comparator;
public class DescendingOrderSortingOfEmployeeArraysInJava8 {
public static void main(String[] args) {
// sample Employee[] array
Employee[] employees = new Employee[]{
new Employee(1001, "Suresh", "Krishna", 25000.00f),
new Employee(1002, "Mani", "Ratnam", 58000.00f),
new Employee(1003, "Balu", "Mahendra", 46000.00f),
new Employee(1004, "Venkat", "Prabhu", 35000.00f),
new Employee(1005, "Lokesh", "Kanagaraj", 75000.00f)
};
// before sorting
System.out.println("Before sorting - Employee[] Arrays\n");
for(Employee emp : employees) {
System.out.println(emp);
}
// sorting Employee[] arrays in descending order
Arrays.sort(employees, Comparator.comparing(Employee::getFirstName).reversed());
// after sorting
System.out.println("\nAfter sorting in Descending-order of first name"
+ " - Employee[] Arrays\n");
for(Employee emp : employees) {
System.out.println(emp);
}
}
}
Output:
Before sorting - Employee[] Arrays
Employee [id=1001, firstName=Suresh, lastName=Krishna, salary=25000.0]
Employee [id=1002, firstName=Mani, lastName=Ratnam, salary=58000.0]
Employee [id=1003, firstName=Balu, lastName=Mahendra, salary=46000.0]
Employee [id=1004, firstName=Venkat, lastName=Prabhu, salary=35000.0]
Employee [id=1005, firstName=Lokesh, lastName=Kanagaraj, salary=75000.0]
After sorting in Descending-order of first name - Employee[] Arrays
Employee [id=1004, firstName=Venkat, lastName=Prabhu, salary=35000.0]
Employee [id=1001, firstName=Suresh, lastName=Krishna, salary=25000.0]
Employee [id=1002, firstName=Mani, lastName=Ratnam, salary=58000.0]
Employee [id=1005, firstName=Lokesh, lastName=Kanagaraj, salary=75000.0]
Employee [id=1003, firstName=Balu, lastName=Mahendra, salary=46000.0]
Note: we need to convert primitive int data-type to Integer wrapper-type by auto-boxing feature introduced in Java 1.5 version, when we pass Comparator object as 2nd argument for reverse order sorting
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles :
- Java 8 – How to sort ArrayList elements ?
- Java 8 – Sorting List of primitive, String & Custom Objects
- Java 8 – Sorting ArrayList using sort() method of List
- Java 8 – How to sort LinkedList elements ?
- Java 8 – How to sort HashSet elements ?
- Java 8 – How to sort LinkedHashSet elements ?
- Java 8 – How to sort TreeSet in descending order ?
- Java 8 – How to sort List and Arrays with null values present ?
- Java 8 – How to sort Arrays in Ascending and Descending order ?
- Java 8 – Sorting list of objects on multiple fields (3-level attributes)
- Java 8 – How to Sort String[] Arrays by its length in Ascending/Descending order ?
- Java 8 – How to Sort String List by its length in Ascending/Descending order ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Happy Coding !!
Happy Learning !!