Java Vector - sort() Method
The java.util.Vector.sort() method is used to sort the elements of the list according to the order induced by the specified comparator.
Syntax
public void sort(Comparator<? super E> c)
Here, E is the type of element maintained by the container.
Parameters
c |
Specify the comparator used to compare list elements. If null, element's natural ordering is used. |
Return Value
void type.
Exception
NA.
Example:
In the example below, the java.util.Vector.sort() method is used to sort the given vector.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> Vec = new Vector<Integer>(); //populating vector Vec.add(50); Vec.add(40); Vec.add(10); Vec.add(30); Vec.add(20); //printing vector System.out.println("Vec contains: "+ Vec); //sorting the vector Collections.sort(Vec); //printing vector System.out.println("Vec contains: "+ Vec); } }
The output of the above code will be:
Vec contains: [50, 40, 10, 30, 20] Vec contains: [10, 20, 30, 40, 50]
❮ Java.util - Vector