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!
Start Free Trial