• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting with Treeset

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have an employee object having fields as employee first name,last name, employee Id , salary, designation .

I have to store it in treeset. But when i want to retrive it , it must be sorted according to employee id.

The problem is that i am unable to sort it according to employee id.(But able to sort according to first name, last name etc using comparable interface).

Please let me now how to do it.

Thanks in advance
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to sort an object use Comparator or Comparable interfaces .to know more search in google
 
RaviSingh Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used the comparable interface. But when i implement the compareTo method i am unable to sort on the basis of employee id since it is integer.

But i am able to sort on the basis of first name, last name etc.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you used Comparable? Its documentation says it implies a "natural ordering" which you don't have for employees. You have several possible orderings, eg by name, seniority, salary, ID number, all of which might be valid. Not one "natural" ordering.

So you want to pass a Comparator<Employee> which simply works out the difference between the ID numbers. If they are ordinary positive ints you can simply subtract the IDs to get a result.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If they are ordinary positive ints you can simply subtract the IDs to get a result.


And if they are not the following works always:
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do one thing put all Employee object in list object and

call this methos Collections.sort(listObject); in that class where you want sorting

put this in Employee class

public int compareTo(Object o) {
Employee e = (Employee ) o;
return this.id.compareToIgnoreCase(e.getId());

}

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the id field is an int, that last code will fail to compile. As I said: not Comparable<Employee> but one or more Comparator<Employee> objects.
reply
    Bookmark Topic Watch Topic
  • New Topic