Sort Java Vector in Descending Order Using Comparator
Vector is a class in Java that is part of the Java Collections Framework. It is similar to an array but can dynamically resize itself to accommodate elements as they are added or removed. The Vector class implements a dynamic array, which means it can grow or shrink in size automatically.
To sort a Java Vector in descending order using a Comparator, you can follow these steps:
- Create a Comparator that defines the sorting order. In this case, you want to sort in descending order, so you can use the Comparator.reverseOrder() method to reverse the elements’ Descending ordering.
- Use the Collections.sort() method to sort the Vector using the Comparator.
Example 1
In the given program, the Comparator.reverseOrder() method creates a Comparator that reverses the natural ordering of the elements. The Collections.sort() method then sorts the Vector using this Comparator, resulting in a sorted Vector in descending order.
FileName: SortVector.java
import java.util.Collections; import java.util.Comparator; import java.util.Vector; public class SortVector { public static void main(String[] args) { // Create a sample Vector Vectorvector = new Vector<>(); vector.add(7); vector.add(5); vector.add(9); vector.add(1); vector.add(3); // Sort the Vector in descending order Comparator comparator = Collections.reverseOrder(); Collections.sort(vector, comparator); // Print the sorted Vector System.out.println(vector); } }
Output
[9, 7, 5, 3, 1]
Example 2
In this program, we create a Vector called a vector and add some integer elements to it. We then define a Comparator using an anonymous inner class that compares two integers in descending order. The compare() method is overridden to compare o2 with o1 using the compareTo() method. This ensures that the elements are sorted in descending order.
Finally, we sort the Vector using the Collections.sort() method, passing the vector and comparator as arguments. The vector is sorted in-place, and we print the sorted Vector using System.out.println(vector).
FileName: SortVector.java
import java.util.Collections; import java.util.Comparator; import java.util.Vector; public class SortVector { public static void main(String[] args) { // Create a sample Vector Vectorvector = new Vector<>(); vector.add(5); vector.add(2); vector.add(8); vector.add(1); vector.add(6); // Sort the Vector in descending order Comparator comparator = new Comparator () { @Override public int compare(Integer o1, Integer o2) { // Compare in descending order return o2.compareTo(o1); } }; Collections.sort(vector, comparator); // Print the sorted Vector System.out.println(vector); } }
Output
[8, 6, 5, 2, 1]
Example 3
In this program, we create a Vector called a vector and add some string elements to it. We define a Comparator using an anonymous inner class that compares two strings in descending order. The compare() method is overridden to compare s2 with s1 using the compareTo() method, resulting in a descending order sorting.
We then sort the Vector using the Collections.sort() method, passing the vector and comparator as arguments. The vector is sorted in place, and we print the sorted Vector by iterating over it using a for-each loop.
FileName: SortVector.java
import java.util.Collections; import java.util.Comparator; import java.util.Vector; public class SortVector { public static void main(String[] args) { // Create a sample Vector of strings Vectorvector = new Vector<>(); vector.add("Apple"); vector.add("Banana"); vector.add("Orange"); vector.add("Mango"); vector.add("Pineapple"); // Sort the Vector in descending order Comparator comparator = new Comparator () { @Override public int compare(String s1, String s2) { // Compare in descending order return s2.compareTo(s1); } }; Collections.sort(vector, comparator); // Print the sorted Vector for (String element : vector) { System.out.println(element); } } }
Output
Pineapple Orange Mango Banana Apple
Example 4
In this Program, we create a Vector called vector that holds Person objects. Each Person object has a name and an age. We define a Comparator using an anonymous inner class that compares two Person objects based on their age in descending order. The compare() method is overridden to compare p2 with p1 using Integer.compare() to compare the ages.
We then sort the Vector using the Collections.sort() method, passing the vector and comparator as arguments. The vector is sorted in place, and we print the sorted Vector by iterating over it using a for-each loop and displaying the name and age of each person.
import java.util.Collections; import java.util.Comparator; import java.util.Vector; class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } public class Main { public static void main(String[] args) { // Create a sample Vector of Person objects Vectorvector = new Vector<>(); vector.add(new Person("Ram", 25)); vector.add(new Person("Sham", 30)); vector.add(new Person("Bhuvan", 20)); vector.add(new Person("Rajesh", 35)); // Sort the Vector in descending order based on age Comparator comparator = new Comparator () { @Override public int compare(Person p1, Person p2) { // Compare in descending order based on age return Integer.compare(p2.getAge(), p1.getAge()); } }; Collections.sort(vector, comparator); // Print the sorted Vector for (Person person : vector) { System.out.println(person.getName() + " - " + person.getAge()); } } }
Output
Rajesh - 35 Sham - 30 Ram - 25 Bhuvan - 20