Sort Java Vector in Descending Order Using Comparator Last Updated : 27 Jan, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Implement the comparator interface in the class and change the natural ordering of the elementsDirectly 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; } } OutputBefore 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); } } OutputBefore sorting elements 4 3 2 6 7 After sorting elements 7 6 4 3 2 Comment More infoAdvertise with us Next Article Sort ArrayList in Descending Order Using Comparator in Java Z zack_aayush Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Vector Java-Comparator +2 More Practice Tags : Java Similar Reads Sort ArrayList in Descending Order Using Comparator in Java A comparator is an interface that is used to rearrange the ArrayList in a sorted manner. A comparator is used to sort an ArrayList of User-defined objects. In java, a Comparator is provided in java.util package. Using Comparator sort ArrayList on the basis of multiple variables, or simply implement 3 min read How to Change the Comparator to Return a Descending Order in Java TreeSet? Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the eleme 3 min read How to Sort Vector Elements using Comparable Interface in Java? Vector is a child interface of collection. If we want to represent a group of the individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for vector. It is a resizable or growable array. It implements a Serializable, Cloneable, and R 5 min read Implementing Sorted Vector in Java Vector is a class that implements the List interface. It is a type of dynamic array that means the size of a vector can be grown or shrink during the execution of the program. The initial size of the vector is 10 and if we insert more than 10 elements then the size of the vector is increased by 100% 3 min read Java Program to Sort Vector Using Collections.sort() Method java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. Syntax: Public void sort(Vector object); Parameters: Instance of the vector as an argument Illustration: Collection.sort() me 2 min read How to Override compareTo() Method in Java? As we know, there are basically two types of sorting technique in Java: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 ascendin 4 min read Like