Java Sorting Arrays

Hi,

All you Java folks must have faced a situation where you require to sort an array of custom objects using Java. Sorting a string array is easy with Java. But, what happens when you have a custom object array, say an array od Person objects to sort. You need to sort them according to a specific custom condition. With java interfaces, this becomes easily doable.

In this example I will use the custom object as 'Car'

public class Car
{
int age;
string id;
string color;
}

I need to sort them according to the age. Thus, according to the ascending order.

First, you need to implemet Comparable interface of java.


public class Car implements Comparable
{
int age;
string id;
string color;
}

So, add the above code in Red. This tells that this class implements the Comparable interface and accepts Car objects only.

Now, you need to actually implement the methods regarding the interface.

public int compareTo(Car o)

You must implement the above method signature and write the relevant code/ logic associated with it.

Note that this method returns an integer. The values which we will use are in the set of {-1,1,0} . So, when we want to sort an array of cars, the JVM will decide on which should be first by looking at the return value. If it is -1, it implies that the second value is larger, if the value returned is 0, it implies both should have the same value.
Since we want to sort them according to the age, we will write the following LOC.

public int compareTo(Car o) {

if (this.age
return -1;
}
else if(o.age==this.age){
return 0;
}
else{
return 1;
}
}

Now, we have done the necessary work to sort. Lets give it a try by using a demo.

First create an array of Car objects and put them in a randomn order.

Car[] carArray= new Car[5];

Car c1 = new Car();
Car c2 = new Car();
Car c3 = new Car();
Car c4 = new Car();
Car c5 = new Car();

c1.setAge(1);
c1.setColor("blue");
c1.setId("");

c2.setAge(2);
c2.setColor("black");
c2.setId("");

c3.setAge(3);
c3.setColor("red");
c3.setId("");

c4.setAge(4);
c4.setColor("green");
c4.setId("");

c5.setAge(5);
c5.setColor("purple");
c5.setId("");


carArray[0]= c4;
carArray[1]= c1;
carArray[2]= c3;
carArray[3]= c5;
carArray[4]= c2;

System.out.println("----------------------------------------------------");
System.out.println("----------------------------------------------------");
System.out.println("Before Sorting");
System.out.println("");


As we observe, the Car objects are not in the correct order. They are in the order of [C4, C1, C3, C5, C2]

NOTE: After the sort, they should be in the order of [C1,C2,C3,C4,C5]

Now, lets print the array to see how they are before we order.

print(carArray);

Now, lets call the sort.

We can call a sort using the Arrays.sort(); method. It belongs to the class Arrays declared in java.utils package. This class has a static method sort, which accepts an array of objects which must implement the comparable interface.
(Since our Car class implemented the interface, we can pass an array of Car objects to this method)

Arrays.sort(carArray);

Now, lets try to print this and see what we get.

print(carArray);

Presto !!!!!! We get an array which is according to the correct order as we required. Thus, by using the Comparable interface in java, we can write almost any custom ordering rule for our classes.
Hope you enjoyed this !!!!!!
Happy coding !!!!

:)

Comments

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline