
-----------------------------------
Naveg
Fri Apr 14, 2006 3:00 pm

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?

-----------------------------------
md
Fri Apr 14, 2006 3:30 pm


-----------------------------------
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
Fri Apr 14, 2006 3:34 pm


-----------------------------------
We need to code our own sort methods - this is for a school project.

-----------------------------------
wtd
Fri Apr 14, 2006 3:59 pm


-----------------------------------
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
Fri Apr 14, 2006 4:07 pm


-----------------------------------
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
Fri Apr 14, 2006 4:16 pm


-----------------------------------
Simply make use of the Comparator object to compare Persons within the Personclass Person {
    public static Comparator compareFirstNames = new Comparator() {
        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: Arrays.sort(persons, Person.compareFirstNames);

-----------------------------------
md
Fri Apr 14, 2006 7:10 pm


-----------------------------------
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 :P

-----------------------------------
wtd
Fri Apr 14, 2006 7:43 pm


-----------------------------------
The method rizzix and I have mentioned is the best option Java currently provides for solving this problem, and also the easiest to implement.

-----------------------------------
zylum
Fri Apr 14, 2006 7:51 pm


-----------------------------------
indeed.  comparator would be the best way  :wink:
