Java Vector sort() Method Tutorial

In this section, we will learn what the Vector sort() method is and how to use it in Java.

What is Java Vector sort() Method?

The Java Vector sort() method, as the name of the method suggests, is used to sort the elements of a Vector object.

For example, you might have a Vector with integer numbers, but these elements are set in the collection with no specific order!

Well, using this method we can sort such list to have a natural order or reverse order, etc.

This method takes a reference to a method and inside that method is the place we run the comparison operation between the elements of a Vector object and decide which element should come after the other.

Basically, the return value of this reference method is of type Integer and it takes two arguments (Two elements of the target collection object at a time).

Now, if in the target reference method the first argument is bigger than the second one, the return value of the method should be +1.

If the first argument is less than the second argument, then the result of this reference method should be -1.

And if the two arguments are equally the same, then the result should be 0.

Using this pattern, the elements of the target Vector will be in natural order.

But we can change the values of +1 and -1 (meaning if the first argument was bigger than the second one, then return -1 instead) in that case, the elements of the target list will be sorted in the reverse order.

Java Vector sort() Method Syntax:

public void sort(Comparator<? super E> c)

sort() Method Parameters:

There’s one parameter for this method and that is of a type the functional interface Comparator.

That means we need to pass a reference to a method that matches the method in this interface.

The signature of this method is:

  • The return value of this method is of type int.
  • The reference method needs to take two arguments and they are of the same type as the elements of the target Vector object.

sort() Method Return Value:

The return value of this method is void.

sort() Method Exceptions:

The method might throw three types of exceptions:

  • ClassCastException: This exception is thrown if the data type of the elements in the target Vector is not compatible with the types of the parameters in the reference method.
  • IllegalArgumentException: We get this exception if the comparator is found to violate the Comparator contract.

Example: using Vector sort() method

import java.util.Vector;

public class Main {

    public static void main(String[] args){
        
        Vector<String> list = new Vector<>();
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("John");

        list.sort((e1,e2)->{
            return e1.compareToIgnoreCase(e2); 
        });
        for (String s: list){
         System.out.println(s);
        }
    }
}

Output:

Ellen

Jack

John

Omid

As you can see, using the sort method, we could sort the elements of this list alphabetically.

Note: check the String compareToIgnoreCase() method if you’re not familiar with this method.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies