The easiest way to sort an array is using the Collections.sort methods. You have a choice of two methods. If your object has a natural order, you can consider to make it implement the Comparable interface (as Manuel suggested). In this case the list can be sorted according to this order. Another option is to implement a Comparator an sort the list according to this, using the second sort method.
For example in this case to me the natural order for Users would be to sort by last name and then first name, so I would probably implement a Comparator to sort by first name and last name, however you might feel different (or your use case might be different).
I ran into a little bit more complicated case, Say I want to Sort users by their attributes for example UserClass has three fields UserStatus (Enum), UserDegree(Enum), Department(Enum)
I would like to sort the userList by several given lists For example: List<User> userList
That would work. I can know - it looks quite a lot like a class I once wrote, except mine had more methods for adding, removing and querying the comparators. The compare method was identical though.
Well, with one exception: I made sure no null comparators were allowed. In this code, I'd check for null first:
For each of the attributes you would like to sort on (e.g. departement, degree and status), you have to write a Comparator<User> that compares the users based on this single attribute. So you end up with three Comparators. Then you create the ComposableComparator<User> and add one or more of these Comparators you have just created. Now the ComposableComparator will order the users based on the Comparators you have added, in the order you added them. If you want the users sorted in a different order, create another ComposableComparator and add the Comparators in a different order.
I'm sure what you want to do with this. If this means the order of the departements, this will come in the Comparator, but you will have to define an order for all possible departement values. If you want to filter on these values, this is not done using Comparators, which is only about sorting. Filtering would be another subject.
You are struggling with the Comparator. Don't confuse Comparator and Comparable. It should be something like (mind you, simplified and no compiler around).
The usage of the ComposableComparator seems ok.
[Fixed bad typo that Garrett mentioned ] [ May 22, 2008: Message edited by: bart zagers ]
Good work. I'm not sure if you know this, but enum types implement Comparable automagically, with their natural ordering being the order in which they are declared. So if the ordering for your Department enum doesn't change, then your Comparator which sorts by Department could look like this:
[ May 22, 2008: Message edited by: Garrett Rowe ]
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.