
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Java Vector in Descending Order Using Comparator
Vectors implement the List interface and are used to create dynamic arrays. The array whose size is not fixed and can grow as per our needs is called as a dynamic array. The Comparator is an interface available in ?java.util' package.
Sorting means rearranging the elements of a given list or array in ascending or descending order. In this article, we will create a vector and then try to sort its elements in descending order using a comparator.
Program to Sort Java Vector in Descending Order
Comparator
As the name suggests it is used to compare something. In Java, the Comparator is an interface that is used to sort custom objects. We can write our own logic inside its inbuilt method named ?compare()' to sort the specified objects. This method takes two objects as arguments and then returns an integer value. By this integer value, Comparator decides which object is greater.
Syntax
Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() { compare( type object1, type object1 ) { // logic for comparison } };
The nameOfComparator passed for sorting operation to methods like ?Collection.sort()'.
Collections.sort() method
The class ?Collections' of the Collection Interface provides a static method named ?Collections.sort()' that can sort elements of specified collections like ArrayList or LinkedList. It is available in ?java.util' package.
Syntax
Collections.sort( nameOfcollection, ComparatorObject );
Collections.reverseOrder()
It returns the comparator in reverse order.
Example 1
In the following example, we will define a vector named ?vectlist' and store a few objects in it by using the ?add()' method. Then, use the Comparator object and ?Collection.sort()' method to sort the vector in descending order.
import java.util.*; public class VectClass { public static void main(String args[]) { // Creation of vector Vector<Integer> vectList = new Vector<>(); // Adding elements in the vector vectList.add(97); vectList.add(93); vectList.add(95); vectList.add(99); vectList.add(82); vectList.add(88); System.out.println("Elements of the unsorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the vector System.out.print(vectList.get(i) + " "); } System.out.println(); // Using comparator interface for sorting Comparator comp = Collections.reverseOrder(); Collections.sort(vectList, comp); System.out.println("Elements of the newly sorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the new vector System.out.print(vectList.get(i) + " "); } } }
Output
Note: VectClass.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Elements of the unsorted list: 97 93 95 99 82 88 Elements of the newly sorted list: 99 97 95 93 88 82
Example 2
In this example, first, we will create a Comparator and inside it, we define our logic in ?compare()' method to sort the vector objects in descending order. The logic here states that take two objects at the same time and compare them using the if-else block. If first object is greater than second return -1 otherwise 1. Then, we pass the object of comparator to ?Collection.sort()' for sorting operation.
import java.util.*; public class VectClass { public static void main(String args[]) { // Using comparator interface for sorting Comparator<Integer> comp = new Comparator<Integer>() { // logic to sort in descending order public int compare(Integer i, Integer j) { if(i < j) { return 1; } else { return -1; } } }; // Creation of vector Vector<Integer> vectList = new Vector<>(); // Adding elements in the vector vectList.add(97); vectList.add(93); vectList.add(95); vectList.add(99); vectList.add(82); vectList.add(88); System.out.println("Elements of the unsorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the vector System.out.print(vectList.get(i) + " "); } System.out.println(); Collections.sort(vectList, comp); // sort using comparator System.out.println("Elements of the newly sorted list: "); // loop to iterate through elements for(int i = 0; i < vectList.size(); i++ ) { // to print the elements of the new vector System.out.print(vectList.get(i) + " "); } } }
Output
Elements of the unsorted list: 97 93 95 99 82 88 Elements of the newly sorted list: 99 97 95 93 88 82
Conclusion
This article has explained the implementation of the Comparator Interface and also we discovered the use of a few inbuilt methods such as ?compareTo()', ?Collection.sort()' and ?Collections.reverseOrder()'.