Array sorting in Java using Comparator Interface

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.

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);
}

}
}

 

ArrayReverseOrder.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());
}
}

o2.toString().compareTo(o1.toString()); is equivalent to:


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;
}

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:


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;
}

Output:


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

 

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:

Array in Ascending Order:
America
London
NeyYork
Paris
Switzerland

Array in Descending Order:
Switzerland
Paris
NeyYork
London
America

 

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

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);
}
}
}

 

Output:


9
6
4
3
2
1

Arrays.sort used with primitive data type “int”: throws compilation error


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);
}
}
}

 

Output:

Compilation error.

 

But still primitive data type array (int array) ascending/descending sorting can be achieved using comparator interface as shown above.

 

 

772 total views, 2 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in


2 thoughts on “Array sorting in Java using Comparator Interface”

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *