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

sorting a vector

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I need to sort a vector. Anyone have any good ideas on how to implement this?

I'm kind of suprised that the JAVA core doesn't supply a sorting method.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it does.

method of java.util.Collections class and java.util.Vector implements java.util.List interface.

Regards,
Susanta
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector now implements the List interface, so you should be able to use the sort capabilities in java.util.Collections. If your elements implement Comparable, then you can use sort(List). Otherwise, you can use sort(List, Comparator).
 
jeff willis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must be doing something wrong.

My vector is a simple list of names.
I populate it in a loop with this statement:



Then I sort it like this:



Then I use it to create a JTable. The elements are not sorted in the table.

Have I missed something?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These days, it's recommended to not use Vector. Use a different type of list, like ArrayList, instead.

If you're curious for the reasoning behind this, I'd recommend a quick search on this forum for "vector arraylist" in the subject. (Note that the search page link is at the top right of this page.) Also, you might like to read the JavaWorld Java Q&A on the subject.

Even if yours is a learning exercise, you might as well learn to use the preferred APIs, while doing it.

Concerning this line of code:

vctAcct.add( new String (acctName) );

Is acctName a String? If so, why are you creating a new String with a String? Are you familiar with what the difference between the following two lines of code are?

String string1 = "myString";
String string2 = new String("myString");

On the issue of sorting, forgetting about the JTable for a moment, have you tried just iterating through the collection, after sorting it, and simply printing it out to the console to see if it got sorted correctly?
 
jeff willis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're on to something there.

I printed out the vector before the sort and after the sort.

It DID appear sorted when the output went to stdout!

I need to examine how I am implementing my table.

...now about the populating of the Vector...

I used the new String() because my string variable was declared inside the loop like this:




Are you saying that this is not necessary?
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trigger the fireTableUpdate event on tableModel interface to update the GUI.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the new String() because my string variable was declared inside the loop
...
Are you saying that this is not necessary?


Yes. I'm also saying that using the String(String) constructor has a particular side effect that you might not be aware of, and you might not be concerned with. Take a look at the String class documentation for a brief explanation of that side effect. This gets to the topic of the String pool, which you might want to start a new thread for, if you have questions about it. I'd also recommend trying a quick search on this forum and on google for the subject.

If you end up with questions about working with a JTable, I'd recommend moseying on over to the Swing / AWT / SWT / JFace forum, and starting a new thread.
[ September 23, 2004: Message edited by: Dirk Schreckmann ]
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic