Skip to content
Related Articles

Related Articles

Improve Article

Sort Java Vector in Descending Order Using Comparator

  • Difficulty Level : Expert
  • Last Updated : 27 Jan, 2021

The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface.

There are two types of Sorting technique:

  • First is internal sorting i.e that uses predefined sorting method ascending order Arrays.sort() for Primitive class arrays and wrapper class arrays and  Collections.sort() for collections both methods sort the elements in ascending order.
  • The second technique is passing the comparator or implement the comparator interface in the class as the second argument in both methods and change the sorting the order according to the requirements. Comparator only works for wrapper class type arrays and for collections like vector, ArrayList, etc.

Input:

vector [4,3,2,6,7]

Output:

vector [2,3,4,6,7]

Approaches to sort vector in descending order using Comparator:



  1. Implement the comparator interface in the class and change the natural ordering of the elements
  2. Directly call the new comparator in the second argument of the Collections.sort() method and change the ordering of the element

Example 1: Overriding the compare method inside the class that implements the Comparator class and then creating and passing the object of that class as a second parameter while calling the Collections.sort() method.

Java




// Java program to Sort Java Vector in
// descending order using comparator
 
import java.io.*;
import java.util.*;
 
// Implement comparator of the Integer class
class GFG implements Comparator<Integer>
{
    // Function to print the elements of the vector
    static void print(Vector<Integer> Numbers)
    {
        for (Integer number : Numbers)
        {
            // Printing the elements
            System.out.print(number + " ");
        }
    }
 
    public static void main(String[] args)
    {
        // Implementing the vector class
        Vector<Integer> elements = new Vector<>();
 
        // Adding elements in the vector class
        elements.add(4);
        elements.add(3);
        elements.add(2);
        elements.add(6);
        elements.add(7);
 
        // Before sorting the elements
        System.out.print("Before sorting elements ");
 
        print(elements);
 
        System.out.println();
 
        // Sorting the vector elements in descending
        // order
        Collections.sort(elements, new GFG());
 
        System.out.print("After sorting elements ");
 
        // Printing the elements
        print(elements);
    }
 
    // Implementing compare function
    @Override public int compare(Integer o1, Integer o2)
    {
        // Changing the order of the elements
        return o2 - o1;
    }
}

 
 

Output
Before sorting elements 4 3 2 6 7 
After sorting elements 7 6 4 3 2

 

Example 2: Overriding the compare function at the time of calling the Collections.sort() method there itself.

 

Java




// Java program to Sort Java Vector in
// descending order using comparator
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to print the elements of the vector
    static void print(Vector<Integer> Numbers)
    {
        for (Integer number : Numbers) {
            // Printing the elements
            System.out.print(number + " ");
        }
    }
 
    public static void main(String[] args)
    {
        // Implementing the vector class
        Vector<Integer> elements = new Vector<>();
 
        // Adding elements in the vector class
        elements.add(4);
        elements.add(3);
        elements.add(2);
        elements.add(6);
        elements.add(7);
 
        // Before sorting the elements
        System.out.print("Before sorting elements ");
 
        print(elements);
        System.out.println();
 
        // Sorting the vector elements in descending
        // order
        Collections.sort(
            elements, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2)
                {
                    // Changing the order of the elements
                    return o2 - o1;
                }
            });
 
        System.out.print("After sorting elements ");
 
        // Printing the elements
        print(elements);
    }
}

 
 

Output
Before sorting elements 4 3 2 6 7 
After sorting elements 7 6 4 3 2

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :