/*
Sort elements of Java Vector Example
This Java Example shows how to sort the elements of java Vector object using
Collections.sort method.
*/
import java.util.Vector;
import java.util.Collections;
public class SortJavaVectorExample {
public static void main(String[] args) {
//create Vector object
Vector v = new Vector();
//Add elements to Vector
v.add("1");
v.add("3");
v.add("5");
v.add("2");
v.add("4");
/*
To sort a Vector object, use Collection.sort method. This is a
static method. It sorts an Vector object's elements into ascending order.
*/
Collections.sort(v);
//display elements of Vector
System.out.println("Vector elements after sorting in ascending order : ");
for(int i=0; i<v.size(); i++)
System.out.println(v.get(i));
}
}
/*
Output would be
Vector elements after sorting in ascending order :
1
2
3
4
5
*/
Bookmark/Search this post with:
Thanks
Thanks very help full
Just an addition if you like to made it reverse
you should use
Comparator r = Collections.reverseOrder();
Collections.sort(v, r); // instead of Collections.sort(v)