Author |
Message |
Naveg
|
Posted: Fri Apr 14, 2006 3:00 pm Post subject: Sorting objects by field |
|
|
This is a question that I'm sure will get quick responses.
Say there is a people, each of which has a first and last name. Using a more elementary approach, I create two parallel arrays, one to store first names and the other, last names. Now, in order to sort by first name, all I have to do is use a generic string sorting method and keep track of the new order, applying it to all other parallel arrays. Simple.
Now, take the same list of people, but this time create an class Person, with two fields, firstName and lastName. Now rather than having two parallel arrays, I have a single array of Persons. How would I sort this array by the firstName of the Persons? I can no longer use a generic string sorting method because the array is not of type String. I could certainly create a static method sortByFirstName in class Person, but this would get unwieldy if I had numerous fields.
What is the most appropriate way to sort an array of Objects by a given field? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
md
|
Posted: Fri Apr 14, 2006 3:30 pm Post subject: (No subject) |
|
|
I know in the C++ stl there is a sort function which will sort anything (it uses < for comparisons by default; but you can pass it your own comparison function). Perhaps there is a similar function in Java? |
|
|
|
|
|
Naveg
|
Posted: Fri Apr 14, 2006 3:34 pm Post subject: (No subject) |
|
|
We need to code our own sort methods - this is for a school project. |
|
|
|
|
|
wtd
|
Posted: Fri Apr 14, 2006 3:59 pm Post subject: (No subject) |
|
|
What you want to do is accept a predicate of some sort. Something which takes two objects and compares them.
In Java we can approximate this with anonymous inner classes.
You may wish to research "Comparator".
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html |
|
|
|
|
|
Naveg
|
Posted: Fri Apr 14, 2006 4:07 pm Post subject: (No subject) |
|
|
Still, I would need a separate method for each field I would ever want to sort by. My question is, is it possible to make a single method that sorts by any field of a given type? |
|
|
|
|
|
rizzix
|
Posted: Fri Apr 14, 2006 4:16 pm Post subject: (No subject) |
|
|
Simply make use of the Comparator object to compare Persons within the Person[] array. For example to sort by first name:
Java: | class Person {
public static Comparator<Person> compareFirstNames = new Comparator<Person> () {
int compare (Person p1, Person p2 ) {
return p1. getFirstName(). compareTo(p2. getFirstName());
}
boolean equals (Object obj ) { return this == obj }
};
} |
Likewise you may create one for compareLastNames etc..
You can then use it like this: Java: | Arrays. sort(persons, Person. compareFirstNames); |
|
|
|
|
|
|
md
|
Posted: Fri Apr 14, 2006 7:10 pm Post subject: (No subject) |
|
|
If you have to write your own sort then you can do whatever you want. The easiest thing would probably be just having a flag which chooses which feild to sort by. I know that's a vague answer... but there are many many ways of doing it |
|
|
|
|
|
wtd
|
Posted: Fri Apr 14, 2006 7:43 pm Post subject: (No subject) |
|
|
The method rizzix and I have mentioned is the best option Java currently provides for solving this problem, and also the easiest to implement. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
zylum
|
Posted: Fri Apr 14, 2006 7:51 pm Post subject: (No subject) |
|
|
indeed. comparator would be the best way |
|
|
|
|
|
|