Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object sorting in a Vector

 
Subbu Palisetty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to sort all objects of same type in a vector depending on any value in those objects. For example, in vector ten emp objects are there and each object have fields like id, sal, age etc. now is it possible to sort them by using age field value(sorted values should be in the same vector)

Thanks in Advance,
Subbu
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd recommend you look at java.util.Collections, which provides a sort method for a List where you can define the Comparator (assuming your Vector objects don't have a natural sort order).
 
Neal Pressley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too have a similar question, I have a vector of objects and I want to sorts it based on different fields of the objects. For example a vector V contains Employee object which has filed like first_name, last_name and id. and now I want to dynamically sort the vector either by first name, last name or id.

The existing code provides me vector and I do not mind it converting to those types of collections which supports sorting, and I do not mind using Comparator, but the main question is I should be able to dynamically supply at the run time which field shall be used for sorting,whether it shall be first name, last name or id?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write separate Comparator implementations for each field, and pick one at runtime; or you can write a single comparator which uses reflection to specify the field name at runtime. The former is faster but involves more classes; the latter is slower, more complex, but fewer classes. It's handy if there are many fields.
Here's a not-especially-good article describing one way to implement the reflection version.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a javaranch post that has working code...

http://www.coderanch.com/t/382093/java/java/Sorting-ArrayList-MemberBean-objects
 
Kamal Mettananda
Ranch Hand
Posts: 51
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neal Pressley:
I too have a similar question, I have a vector of objects and I want to sorts it based on different fields of the objects. For example a vector V contains Employee object which has filed like first_name, last_name and id. and now I want to dynamically sort the vector either by first name, last name or id.

The existing code provides me vector and I do not mind it converting to those types of collections which supports sorting, and I do not mind using Comparator, but the main question is I should be able to dynamically supply at the run time which field shall be used for sorting,whether it shall be first name, last name or id?


You can use the java.util.Collections class for this. Check the following methods.

1). Collections.sort(List);
2). Collections.sort(List, Comparator);

First one is used for sorting the objects in List according to the default order. But the second method can be used to sort the objects in any specific order at runtime. You just have to call that method using the correct Comparator instance to change the sorting order.

This (article) explains the use Comparators and Comparables. Good news is, it also uses a similar Employee object, and different Comparators and Comparables.

HTH.
[ August 13, 2008: Message edited by: Kamal Mettananda ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic