+ Reply to Thread
Results 1 to 5 of 5

Thread: Sorting Arrays of Objects with Comparable

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Sorting Arrays of Objects with Comparable

    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.

    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;
    	}
    }
    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:
    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");
    We populate an array of Person objects. Now we want to sort this array and output some information about it.

    First let us look at how we are going to do some outputting.

    Add this method to the Person class.

    Code:
    public String toString() {
    	return name + " is " + age + " years old and has a height of " + height;
    }
    Whenever we pass an object to the System.out.println method the toString method is called and that is what is outputted.

    Example:

    Code:
    System.out.println(aroPeople[0]);
    Output:

    James is 17 years old and has a height of 100
    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.

    Change this line:

    Code:
    class Person {
    to

    Code:
    class Person implements Comparable<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:
    public int compareTo(Person p) {
            
            return 0;
    }
    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).

    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:

    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;
        }
    This gives you a very fast and quick way to sort an array of objects by any fields in any order that you want.

    Now to do the sorting and outputting:

    Code:
    Arrays.sort(aroPeople);
    for (int i=0;i<aroPeople.length;i++) {
                System.out.println(aroPeople[i]);
    }
    All the details of the sorting is handled by Java and you don't need to know why it works. Hope this helps!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Sorting Arrays of Objects with Comparable

    Well done, Chili! +rep

  4. #3
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Sorting Arrays of Objects with Comparable

    Very Nice +rep

  5. #4
    Phazic is offline Newbie
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Re: Sorting Arrays of Objects with Comparable

    Thanks for the nice tutorial, made me get this in like 5minutes x)

  6. #5
    Java_'s Avatar
    Java_ is offline Newbie
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    13
    Rep Power
    0

    Re: Sorting Arrays of Objects with Comparable

    Thanks inspired me to do something with my applet. +Rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting Values from Arrays
    By An Alien in forum Java Help
    Replies: 13
    Last Post: 03-06-2011, 06:36 PM
  2. Sorting Arrays
    By chili5 in forum Java Tutorials
    Replies: 11
    Last Post: 02-07-2010, 12:45 PM
  3. Need help sorting 2 arrays.
    By posto2012 in forum C and C++
    Replies: 3
    Last Post: 10-11-2009, 04:46 PM
  4. Sorting Objects
    By chili5 in forum Java Help
    Replies: 4
    Last Post: 12-26-2008, 02:08 PM
  5. Sorting Arrays
    By falco85 in forum PHP Development
    Replies: 5
    Last Post: 08-23-2006, 07:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts