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