Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaSort Java Vector in Descending Order Using Comparator

Sort Java Vector in Descending Order Using Comparator

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

 

RELATED ARTICLES

Most Popular

Recent Comments