• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Sort array - by two fields

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have List of Users : List<User>
Each User have First-Name and Last-Name,
How do I sort the list by First-Name and then Last-Name?

Thank you
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your User class must implement the interface Comparable. Check this example:

Interface Comparable
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
It's working great!

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

MySorter.addSorter(Department,list<Department>(R&D,Accounting,Testing))
MySorter.addSorter(UserStatus ,list<UserStatus>(OutSourcing, fixed, unemployed))
MySorter.addSorter(UserDegree,list<UserDegree>(15,44,55))

List<User> newUserList = MySorter.sort(userList)


So newUserList will come out sorted first by Department then by UserStatus and last by UserDegree (according to the types in the list.)

In each one of the first level sort the users will come out sorted first by R&D and in R&D all OutSourcing


I will be glad to have some help.

Sharon
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do something like this :
(Note: this suggestion is untested)

[ May 21, 2008: Message edited by: Garrett Rowe ]
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Garrett
Thank you for the code,I am not sure how to use the ComposableComparator,
I made little sample to run sort on 1000 random users,

can you tell me how to set up the ComposableComparator code in SortRunner class?

Thanks again
Sharon

Class SortRunner



Class User :
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am missing something with the Generics,
should it be :



and then :

 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously Bart means:
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!!!
Its working nicely, little bit polishing and enhancing and its perfect!!!


DepartmentComparator looks like :
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic