Sorting Objects
The java.util packages contains a class Array that contains methods for sorting arrays. It can sort arrays of any type and uses a modified quick sort algorithm to sort the array.
Let us take a look at sorting objects. To do this we have to lean about the Comparable interface. The comparable interface describes how two objects should be compared. This interface is what allows us to sort arrays.
First we have to create a class. For the purpose of this tutorial we will create a Person class with three fields name, age, and height.
This code is very simple. It is a template to create a bunch of Person objects. We need an array of them so we will create an array of people.Code:class Person { int age; int height; String name; Person(int age, int height, String name) { this.age = age; this.height = height; this.name = name; } }
We populate an array of Person objects. Now we want to sort this array and output some information about it.Code:Person[] aroPeople = new Person[4]; aroPeople[0] =new Person(17,100,"James"); aroPeople[1] = new Person(18,94,"John"); aroPeople[2] = new Person(16,120,"Chris"); aroPeople[3] = new Person(17,89,"Bob");
First let us look at how we are going to do some outputting.
Add this method to the Person class.
Whenever we pass an object to the System.out.println method the toString method is called and that is what is outputted.Code:public String toString() { return name + " is " + age + " years old and has a height of " + height; }
Example:
Output:Code:System.out.println(aroPeople[0]);
Now that we have output down, all we have left to do is tell Java how we want to sort the array. This is what we need the Comparable interface for.James is 17 years old and has a height of 100
Change this line:
toCode:class Person {
This tells Java that we are going to be comparing Person objects. Now we have to implement the compare to method which tells us how we want to do some sorting.Code:class Person implements Comparable<Person> {
Add that method to the class declaration. We will return 1 if the object we are comparing to is greater than the current object, -1 if the object we are comparing to is less than the current object. Otherwise, we will return 0 (indicating that they are equal).Code:public int compareTo(Person p) { return 0; }
Returning 1 will sort in ascending order, returning -1 will sort in descending order. This approach allows us to sort by more than one field.
We want to sort in ascending order by age, then descending order by height. Only if age is equal do we want to sort by height.
Change the compare to method to this:
This gives you a very fast and quick way to sort an array of objects by any fields in any order that you want.Code:public int compareTo(Person p) { if (age < p.age) { return -1; } if (age > p.age) { return 1; } // only sort by height if age is equal if (height > p.height) { return -1; } if (height < p.height) { return 1; } return 0; }
Now to do the sorting and outputting:
All the details of the sorting is handled by Java and you don't need to know why it works. Hope this helps!Code:Arrays.sort(aroPeople); for (int i=0;i<aroPeople.length;i++) { System.out.println(aroPeople[i]); }
Well done, Chili! +rep
Thanks for the nice tutorial, made me get this in like 5minutes x)
Thanks inspired me to do something with my applet. +Rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks