• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting an ArrayList containing objects  RSS feed

 
Tresa Rose
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an ArrayList containing some Person objects.
The Person class has the following attributes, all of which are of type
String. (A person may have multiple roles.)

p_id
p_name
p_role
p_manager
p_salary

Based on a flag, this ArrayList should be sorted using multiple fields.
That is ,


Can you please explain how the ArrayList can be sorted on multiple attributes.

Many thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across the Collections class? It has a sort method you can apply to a List. Sounds like you might also need to implement a Comparator to define your sorting rules.
 
Tresa Rose
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul for your inputs.
I came across something like this.



Please advise if there is a better way of doing the same in terms of performance and maintainability of code.

Many Thanks
Tresa
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!