Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting objects by member fields

Posted on 2008-06-23
6
Medium Priority
?
229 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

783 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