I cant find any sorting function in the java API for vectors.
Collections.sort is only for List<T> and not for Vector<T>.
I don’t want to write my own sorting function because I think java should implement this.
I’m looking for something like:
class ClassName implements Comparator<ClassName> ..
ClassName cn = ..;
sort(cn);
As per the API docs,
Vectorjust implementsList, so I don’t forsee problems. Maybe your confusion was caused because you declaredVectoraccording the old Java 1.0 style:instead of the declaring it aginst the interface (which is considered good practice):
You can thus just make use of
Collections#sort()to sort a collection,Comparableto define the default ordering behaviour and/orComparatorto define an external controllable ordering behaviour.Here’s a Sun tutorial about ordering objects.
Here’s another SO answer with complete code examples.
That said, why are you still sticking to the legacy
Vectorclass? If you can, just replace by the improvedArrayListwhich was been designed as replacement ofVectormore than a decade ago.