This example shows how to sort a Vector in Java. This example also shows how to sort Vector in ascending or descending order using the sort method of the Collections class, Comparable, and Comparator.
How to sort a Vector in Java?
We can sort elements of the vector object using the sort
method of the Collection class.
1 |
public static <T extends Comparable<? super T>> void sort(List<T> list) |
This sort
method sorts the specified list elements in their natural order. Since the Vector class has implemented the List interface, we can use this method to sort its elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(50); vNumbers.add(40); vNumbers.add(20); vNumbers.add(10); vNumbers.add(30); /* * The sort method sorts the vector * elements in their natural order */ Collections.sort(vNumbers); System.out.println( vNumbers ); |
Output
1 |
[10, 20, 30, 40, 50] |
As we can see from the output, the vector elements are sorted in the natural order i.e. ascending order for the type Integer.
How to sort vector elements in descending order?
The overloaded sort
method of the Collection class also accepts a Comparator object.
1 |
public static <T> void sort(List<T> list, Comparator<? super T> c) |
We can pass a Comparator obtain using the reverOrder
method of the Collections class to this method to sort the elements in descending order as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(50); vNumbers.add(40); vNumbers.add(20); vNumbers.add(10); vNumbers.add(30); /* * To sort the vector in descending order, * obtain a reverse comparator using the * reverseComparator method of the Collections * class */ Collections.sort(vNumbers, Collections.reverseOrder()); System.out.println( vNumbers ); |
Output
1 |
[50, 40, 30, 20, 10] |
How to sort a Vector of custom class objects?
The Collections sort
method works only if the list element class has implemented the Comparable interface. If the class has not implemented the Comparable interface, we get a compilation error “The method sort(List<T>) in the type Collections is not applicable for the arguments (Vector<ClassName>)”.
If the Vector elements are objects of a custom class, then the custom class must implement the Comparable interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.Collections; import java.util.Vector; class Student implements Comparable<Student>{ private int id; public Student(int id){ this.id = id; } public String toString(){ return "Student[" + this.id + "]"; } public int getId(){ return this.id; } public int compareTo(Student otherStudent) { return this.getId() - otherStudent.getId(); } } public class SortJavaVectorExample { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(103)); vStudents.add(new Student(101)); vStudents.add(new Student(102)); Collections.sort(vStudents); System.out.println(vStudents); } } |
Output
1 |
[Student[101], Student[102], Student[103]] |
To sort the elements in the descending order, we can pass the Comparator obtained using the Collections reverseComparator
method as given below.
1 2 3 |
Collections.sort(vStudents, Collections.reverseOrder()); System.out.println(vStudents); |
Output
1 |
[Student[103], Student[102], Student[101]] |
How to sort a Vector of custom class objects using the custom Comparator?
Instead of implementing a Comparable interface, we can also create a custom Comparator for the objects of a custom class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.util.Collections; import java.util.Comparator; import java.util.Vector; class Student{ private int id; public Student(int id){ this.id = id; } public String toString(){ return "Student[" + this.id + "]"; } public int getId(){ return this.id; } } //this will sort Student objects in ascending order class StudentComparator implements Comparator<Student>{ public int compare(Student s1, Student s2) { return s1.getId() - s2.getId(); } } //this will sort Student objects in descending order class StudentComparatorDescending implements Comparator<Student>{ public int compare(Student s1, Student s2) { return s2.getId() - s1.getId(); } } public class SortJavaVectorExample { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); vStudents.add(new Student(103)); vStudents.add(new Student(101)); vStudents.add(new Student(102)); //sort objects in ascending order using comparator Collections.sort(vStudents, new StudentComparator()); System.out.println(vStudents); //sort objects in descending order using comparator Collections.sort(vStudents, new StudentComparatorDescending()); System.out.println(vStudents); } } |
Output
1 2 |
[Student[101], Student[102], Student[103]] [Student[103], Student[102], Student[101]] |
This example is a part of the Java Vector Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation