Solved

Sorting objects by member fields

Posted on 2008-06-23
6
222 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 51
egit plugin on eclipse 8 82
String array comparison 4 38
difference between sorce folder and folder in eclipise 3 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

830 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