Array sorting in Java using Comparator Interface
Table of Contents
Array sorting in Java using Comparator Interface
Array can be sorted in ascending and descending order using,
- Arrays.sort() method.
- By Implementing Comparator interface.
Find the example array sorting programs below.
With Comparator Interface Array sorting Example:
ArraySorting.java
Java File with integer array and Called ArrayReverseOrder and ArrayAscendingOrder using Comparator implemented classes to perform descending and ascending array sorting respectively.
[java]
package in.javadomain;
import java.util.Arrays;
public class ArraySorting {
public static void main(String[] args) {
Integer[] a = { 3, 5, 1, 4, 2, 6 };
System.out.println(“Input given Array”);
for (int i : a) {
System.out.println(i);
}
Arrays.sort(a, new ArrayReverseOrder());
System.out.println(“\n\n”);
System.out.println(“Sorting Array in Descending Order”);
for (int i : a) {
System.out.println(i);
}
Arrays.sort(a, new ArrayAscendingOrder());
System.out.println(“\n\n”);
System.out.println(“Sorting Array in Ascending Order”);
for (int i : a) {
System.out.println(i);
}
}
}
[/java]
ArrayReverseOrder.java:
[java]
package in.javadomain;
import java.util.Comparator;
public class ArrayReverseOrder implements Comparator {
@Override
public int compare(Object o1, Object o2) {
return o2.toString().compareTo(o1.toString());
}
}
[/java]
o2.toString().compareTo(o1.toString()); is equivalent to:
[java]
if (Integer.parseInt(o1.toString()) > Integer.parseInt(o2.toString())) {
return -1;
} else if (Integer.parseInt(o1.toString()) < Integer.parseInt(o2.toString())) {
return 1;
} else {
return 0;
}
[/java]
ArrayAscendingOrder.java:
package in.javadomain;
import java.util.Comparator;
public class ArrayAscendingOrder implements Comparator {
@Override
public int compare(Object o1, Object o2) {
return o1.toString().compareTo(o2.toString());
}
}
[/java]
o1.toString().compareTo(o2.toString()); is equivalent to:
[java]
if (Integer.parseInt(o1.toString()) > Integer.parseInt(o2.toString())) {
return 1;
} else if (Integer.parseInt(o1.toString()) < Integer.parseInt(o2.toString())) {
return -1;
} else {
return 0;
}
[/java]
Output:
[plain]
Input given Array
3
5
1
4
2
6
Sorting Array in Descending Order
6
5
4
3
2
1
Sorting Array in Ascending Order
1
2
3
4
5
6
[/plain]
Arrays.sort() method Java Example:
package in.javadomain;
import java.util.Arrays;
import java.util.Collections;
public class ArraysSort {
public static void main(String[] args) {
String a[] = { “America”, “London”, “Paris”, “NeyYork”, “Switzerland” };
Arrays.sort(a);
System.out.println(“Array in Ascending Order:”);
for (String temp : a) {
System.out.println(temp);
}
System.out.println(“\n\n”);
Arrays.sort(a,Collections.reverseOrder());
System.out.println(“Array in Descending Order:”);
for (String temp : a) {
System.out.println(temp);
}
}
}
Output:
[plain]
Array in Ascending Order:
America
London
NeyYork
Paris
Switzerland
Array in Descending Order:
Switzerland
Paris
NeyYork
London
America
[/plain]
Remember:
Arrays.sort can not used for primitive data types[like int, double,float etc..].
But it can be used for objects and wrapper classes [like Integer,Double,Float etc..]
Arrays.sort used with Wrapper class object “Integer”: Works fine
[java]
package in.javadomain;
import java.util.Arrays;
import java.util.Collections;
public class ArraySortingJava8 {
public static void main(String[] args) {
Integer[] a = { 1, 2, 6, 3, 9, 4 };
Arrays.sort(a, Collections.reverseOrder());
for (int i : a) {
System.out.println(i);
}
}
}
[/java]
Output:
[plain]
9
6
4
3
2
1
[/plain]
Arrays.sort used with primitive data type “int”: throws compilation error
[java]
package in.javadomain;
import java.util.Arrays;
import java.util.Collections;
public class ArraySortingJava8 {
public static void main(String[] args) {
int[] a = { 1, 2, 6, 3, 9, 4 };
Arrays.sort(a, Collections.reverseOrder());
for (int i : a) {
System.out.println(i);
}
}
}
[/java]
Output:
Compilation error.
But still primitive data type array (int array) ascending/descending sorting can be achieved using comparator interface as shown above.
How to sort multiple columns of a class.
Suppose I have Employee is pojo a class Has age,firstname,lastname & salary. Want to sort on multiple fields.
Good Question Sid.
Posted a fresh post for your requirement here,
http://www.ngdeveloper.com/multiple-field-sorting-using-comparator/