VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-sort-vector-elements-using-comparable-interface-in-java/

⇱ How to Sort Vector Elements using Comparable Interface in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Sort Vector Elements using Comparable Interface in Java?

Last Updated : 23 Jul, 2025

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 RandomAccess interface. Every method present in the vector is synchronized and hence vector object is thread-safe.

Comparable interface is used to order the objects of the user-defined class. This interface is present in java.lang' package and it contains only one method compareTo(object). But it provides only a single sorting sequence i.e we can sort elements on the basis of single data members.

compareTo() method

This method is used to compare the current object with the specified object, and it returns three values depending on the comparison of the two objects. Using Comparable interface we can sort the elements of String class objects, user-define class objects, and all wrapper class objects.

Syntax:

public int compareTo(Object obj); 

Return Type: Integer value

  • Negative value if and only if the first object has to come before the second object.
  • Positive value if and only if the first object has to come after the second object.
  • Zero if and only if the first object is equal to the second object.

Collections is a utility class present in java.util' package to define several utility methods for collection objects like sorting, searching and reversing, etc.

In order to sort elements of a list, the Collections class defines the two sort methods as follows:

  1. public static void sort(List l): To sort based on default natural sorting order. In this case, a list should contain a homogeneous object or comparable object otherwise we will get runtime exception also list should not contain null otherwise we will get NullPointerException.
  2. public static void sort(List l, Comparator c): To sort based on customized sorting orders.

Implementation:

Consider creating a custom class name Students which implements the comparable interface in which we get the id of each student. Inside the driver class, we create a vector of type student class, and then we can sort the vector elements using the comparable interface by comparing the students' id. 

Example 1:


Output
[Student[1], Student[2], Student[9], Student[4], Student[34]]
[Student[1], Student[2], Student[4], Student[9], Student[34]]

Example 2: student name, marks, and id in which we are going to sort the vector on the basis of student marks 


Output
Name:Roshan->Marks:86->ID:1
Name:Ritik->Marks:96->ID:2
Name:Ashish->Marks:99->ID:4
Name:Sandeep->Marks:100->ID:9
Name:Piyush->Marks:88->ID:34

Name:Roshan->Marks:86->ID:1
Name:Piyush->Marks:88->ID:34
Name:Ritik->Marks:96->ID:2
Name:Ashish->Marks:99->ID:4
Name:Sandeep->Marks:100->ID:9


 

Comment