It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

sorting of atributes of userdefined array object

sorting of atributes of userdefined array object

Postby deepali.bhosale on Wed Feb 21, 2007 10:52 am

hi,
I have an array of Student Object. Student has name, age and phone number attribute. I need to sort this array on the basis on name ,age and Phone number .
How to do that.
I have read about Arrays.sort().
But how to apply this on Student arrays' attributes.
plz help...
Thanks in advance
Deepali
deepali.bhosale
 
Posts: 0
Joined: Wed Feb 21, 2007 10:48 am

Postby ManUnderground on Wed Feb 21, 2007 11:11 pm

Two options, either have the student object implement the comparator interface thus defining the compare(o1, o2) method. It would something like this:

class Student implements Comparator<Student> {
....
}

Or you can create a stand alone comparator class/code block and pass that as a second parameter to sort.

It's probably worth the first approach if you want students sorted in other areas too.
ManUnderground
 
Posts: 0
Joined: Wed Feb 21, 2007 11:08 pm


Who is online

Users browsing this forum: No registered users and 1 guest