import java.util.Vector;
import java.util.Collections;
import java.util.Comparator;
public class SortVectorExample {
public static void main(String[] args) {
//create a Vector object
Vector v = new Vector();
//Add elements to Vector
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
/*
To get comparator that imposes reverse order on a Collection use
static Comparator reverseOrder() method of Collections class
*/
Comparator comparator = Collections.reverseOrder();
System.out.println("Before sorting Vector in descending order : " + v);
/*
To sort an Vector using comparator use,
static void sort(List list, Comparator c) method of Collections class.
*/
Collections.sort(v,comparator);
System.out.println("After sorting Vector in descending order : " + v);
}
}
Output:
Before sorting Vector in descending order : [1, 2, 3, 4, 5]
After sorting Vector in descending order : [5, 4, 3, 2, 1]
import java.util.Collections;
import java.util.Comparator;
public class SortVectorExample {
public static void main(String[] args) {
//create a Vector object
Vector v = new Vector();
//Add elements to Vector
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
/*
To get comparator that imposes reverse order on a Collection use
static Comparator reverseOrder() method of Collections class
*/
Comparator comparator = Collections.reverseOrder();
System.out.println("Before sorting Vector in descending order : " + v);
/*
To sort an Vector using comparator use,
static void sort(List list, Comparator c) method of Collections class.
*/
Collections.sort(v,comparator);
System.out.println("After sorting Vector in descending order : " + v);
}
}
Output:
Before sorting Vector in descending order : [1, 2, 3, 4, 5]
After sorting Vector in descending order : [5, 4, 3, 2, 1]
0 comments:
Post a Comment