Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Mulitple sorting on different properties

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Folks,

I have an employee class, which have properties like empName and empAge. Both empName ans empAge can be duplicated.
And I have a list of employee objects, which contains duplicate empNames and empAge.
Now I want sort the List object based on empName and then based on empAge(if duplicate empName is there in the list).

Basically I want to know how to do mulitple sort on different properties, if I have duplicate values for a same property.

Note : I know that I have to use Comparator or Comparable for sorting based on sigle property ( like empName)
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note : I know that I have to use Comparator or Comparable for sorting based on sigle property ( like empName)


You can sort on more than one property in the compare method. Compare the empName, and if they are equal, compare the empAge.
 
Durga Krishna
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That is also fine.

I want to know whether any Interface is available in Java API to do this knid of sorting.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a helper class called org.apache.commons.lang.builder.CompareToBuilder in Apache Commons Lang. But you don't need any third party tools to do this. It's just a couple of if blocks.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Durga Krishna wrote:
That is also fine.

I want to know whether any Interface is available in Java API to do this knid of sorting.



As the first reply said - the Comparator Interface. Look at the Javadoc for java.util.Comparator and for java.util.Collections (the sort() method).
 
reply
    Bookmark Topic Watch Topic
  • New Topic