• 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

vector sorting

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi;
I need to have program for sorting a vector based on a parameter of an object which is an element of the vector.
For example, if I have "Employee" as the object. I want to sort the entire vector based on "First name" which is an attribute of the object. Here instance of "Employee" object is an element of the vector.
I require this very urgently.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of ways you can do this:
You could write your own sort routine that takes a vector and sorts it based on a criteria.
Or
You could convert the vecotr to an array and sort it using one of the sort method in the Arrays class. You'd have to create a comparator to do it but if your vector is fairly large this would probably be more efficient than writiing your own sort method. something like this:

The sort method will use the comparator to base it sorting decisions on. Then all you need to do is change the vector an array and call sort on it:

now tempArr holds the sorted array based ont he comparator you passed to the sort method. You might have to play with it a little to get it to work (I took this from an old project I did). But this should give you a god start.
hope it helps


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Vick:

You could convert the vecotr to an array and sort it using one of the sort method in the Arrays class. You'd have to create a comparator to do it but if your vector is fairly large this would probably be more efficient than writiing your own sort method. something like this:


Rather than convert it to an array, you could just use the Collections sort method. To modify your example:

Next, without changing the Vector to an array:

 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill
Absolutely right!! I forgot all about that...

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that String is Comparable as well!If you want to get fancy, you can roll a Comparator that takes an attribute in the constructor and uses reflection to retrieve that attribute (which must be Comparable) and compare it.
- Peter
[This message has been edited by Peter den Haan (edited November 22, 2001).]
 
moor krish
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could able crack with the help of your suggestions. Now I want to generalize the program so that if I pass the entity name and attribute of the entity , it will substituite and produce the results. Otherwise I need to replicate this program for each of my report. Assume I hold all the values required for my report in an entity bean and I need to sort based on any one of the parameters of the entity bean.
 
moor krish
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could able crack with the help of your suggestions. Now I want to generalize the program so that if I pass the entity name and attribute of the entity , it will substituite and produce the results. Otherwise I need to replicate this program for each of my report. Assume I hold all the values required for my report in an entity bean and I need to sort based on any one of the parameters of the entity bean
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this. Untested code, partly cut & paste from existing code that did something rather different.HTH,
- Peter
[This message has been edited by Peter den Haan (edited November 29, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic