21 Sept 2012

sorting vector in ascending order

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:
Vector elements after sorting in ascending order :
1
2
3
4
5

0 comments:

Post a Comment