Alphabetically Order Vectors Elements
Does anyway know of a method which will do this on a vector, or a container which automatically sorts them? Otherwise i might try and use the ascii values. But just wondering what you peeps thought?
MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Check the collections API, there are some sorted implementations you can use.
No sorted List is provided but it isn't that hard to make one if you use another collection to back it which is sorted itself (or use that one directly if you have no need of the List interface per se).
And before I forget: don't use Vector. It's an old relic and has long since been replaced by the far more efficient ArrayList.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
If you use an ArraList like jwenting suggested, you can use:
Collections.sort(ArrayList);
You might could use that on a vector, but I doubt it. Never tried.
Also, isn't there some way to use the comparator interface to sort?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
I actually used a SortedSet which seems to have done the job nicely. Thanks for the tip on Vectors, i see they have synchronisation overhead - what other problems have you found with them. I couldn't find a clear explanation. Thanks again guys :)
MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
The performance penalty alone is enough reason.
The rest is historical. Until recently Vector was not part of the Collections framework. It still is an oddball with regard to implementing methods of the framework.
Personally I don't understand why Sun didn't just deprecate the class seeing as a perfectly functional alternative is available.
ArrayList is a far cleaner as well as better performant solution.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337