• 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

object sorting in a Vector

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a javaranch post that has working code...

https://coderanch.com/t/382093/java/java/Sorting-ArrayList-MemberBean-objects
 
Ranch Hand
Posts: 51
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic