aspose file tools*
The moose likes Java in General and the fly likes object sorting in a Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "object sorting in a Vector" Watch "object sorting in a Vector" New topic
Author

object sorting in a Vector

Subbu Palisetty
Greenhorn

Joined: May 15, 2008
Posts: 6
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

Joined: Mar 22, 2008
Posts: 265

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).


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Neal Pressley
Greenhorn

Joined: May 29, 2008
Posts: 14
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
Here is a javaranch post that has working code...

http://www.coderanch.com/t/382093/java/java/Sorting-ArrayList-MemberBean-objects


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

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 ]

Kamal Mettananda - SCJP 1.4 (98%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object sorting in a Vector
 
Similar Threads
Sorting a Vector by variable in the Object
Sorting a Vector by a specific language?
Sorting a Vector by a specific language?
Sorting a Vector by a specific language?
put Data from Vector into DB.