Solved

Sorting objects by member fields

Posted on 2008-06-23
6
220 Views
Last Modified: 2010-03-30
Hello,

I have a simple question about sorting an array of objects based on any one of the member variables contained within that object.  Assume I have:

public class Person {
  protected int personNumber;
  protected String personName;

  // and then some set/get methods
  // and toString methods etc
}

public class PersonTest {
  public static void main (String[] args) {
    Person person[]=new Person[10];
    // and some code to define all of the people

    // and then a call to sort by personNumber
    // print the object (toString)

    // and then a call to sort by personName
    // print the object (toString)
  }
}

Using Arrays.sort or Collections.sort (I'm a novice Java programmer, so please be patient with me), can someone provide me a simple example of how to sort by personNumber and how to sort by personName?  My reference texts are not providing me much of any usable assistance on the topic.

Also any links to very thorough documentation and examples online would be much appreciated.  Just looking for guidance on steps to take to do this.

Thanks!
 
0
Comment
Question by:jchristn123
  • 3
  • 3
6 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 21852710
Use collection like List to store the Person obj and use Collections.sort with custom comparator...

for e.g.,

class PersonComparatorByName implements Comparator<Person>{
  public int compare( Person p1, Person p2 ){
    return p1.getPersonName().compareTo( p2.getPersonName() ) ;
  }
}

ArrayList<Person> persons = new ArrayList<Person>() ;
//populat the list with person objects
Collections.sort( persons, new PersonComparatorByName () ) ;
0
 
LVL 1

Author Comment

by:jchristn123
ID: 21852787
Do I need to manually create the compareTo method?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 21852789
you need not, compareTo method already exists in String
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:jchristn123
ID: 21852808
Thanks - what if I've already created the objects like this:

person[someNumber] = new person(number, name);

How would I turn that into a person definition within the list?

Thanks!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 21852818
List<Person> list = Arrays.asList( person ) ;
0
 
LVL 1

Author Comment

by:jchristn123
ID: 21862494
I was able to continue using Arrays.sort by using the index parameters, i.e. Arrays.sort(object, 1, n) where n is the upper array boundary.  Worked like a champ
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
word0 challenge 3 77
Image decoding from Camera 3 72
Oracle SQL syntax check  without executing 6 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now