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:
- Implement the comparator interface in the class and change the natural ordering of the elements
- 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
import java.io.*;
import java.util.*;
class GFG implements Comparator<Integer>
{
static void print(Vector<Integer> Numbers)
{
for (Integer number : Numbers)
{
System.out.print(number + " " );
}
}
public static void main(String[] args)
{
Vector<Integer> elements = new Vector<>();
elements.add( 4 );
elements.add( 3 );
elements.add( 2 );
elements.add( 6 );
elements.add( 7 );
System.out.print( "Before sorting elements " );
print(elements);
System.out.println();
Collections.sort(elements, new GFG());
System.out.print( "After sorting elements " );
print(elements);
}
@Override public int compare(Integer o1, Integer o2)
{
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
import java.io.*;
import java.util.*;
class GFG {
static void print(Vector<Integer> Numbers)
{
for (Integer number : Numbers) {
System.out.print(number + " " );
}
}
public static void main(String[] args)
{
Vector<Integer> elements = new Vector<>();
elements.add( 4 );
elements.add( 3 );
elements.add( 2 );
elements.add( 6 );
elements.add( 7 );
System.out.print( "Before sorting elements " );
print(elements);
System.out.println();
Collections.sort(
elements, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2)
{
return o2 - o1;
}
});
System.out.print( "After sorting elements " );
print(elements);
}
}
|
Output
Before sorting elements 4 3 2 6 7
After sorting elements 7 6 4 3 2
Last Updated :
27 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...