Java Question: Vector Sorting

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
Okay, I'm using a few Vectors in my code and I need to sort a few of them. To make it simple I was using the .toArray() method since a Vector inherents it from the Collection class. Then I can use the .sort() method that Arrays have. Then I put what I sorted back into the Vector. I could make my own recursive method with for loops to traverse the Vector list and do a bubble or swap sort or something like that. But why bother when it's already been done?

Anyhow, my boss tells me there is a sort method already done for the Collection class. I can't find it anywhere in the Java docs on java.sun.com. He's not here today to ask what he was talking about.

Anyone know if there is a way to sort Vectors from a Collection's method?
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Collections.sort() method

You can pass the Vector by itself and it will do a default alphabetical sort or pass in a Comparator of your own to sort on speicific field sof custom objects.
 

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
Yah, just saw it.. was looking at the Collection class, not the Collections class.. Argh!
 

Stuxnet

Diamond Member
Jun 16, 2005
8,392
1
0
You can do some pretty quick sorting with the Comparator class, too... obviously not specific to Vectors.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
What you mean is that you can sort on different criteria with Comparator but still using Collections.sort, right? Implementing Comparator won't sort anything for you. Just be careful if you're combining that approach with generics, it can get ugly pretty quickly :p