Java Program to sort objects using Comparable Interface

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at a Java Program to sort objects using Comparable Interface.

Consider a Student object having properties like rollno, name and marks. We will try to sort the objects based on rollno and name.

Below is the Student class definition:

The Student class needs to

  • implement the Comparable interface for sorting the objects.
  • define the compareTo method to sort based on rollno and then on name properties.

 

Here is the Java program to sort the Students collection:

Output:

 

 

 

 

 

 

 

Happy Learning :).

Please leave a reply in case of any queries.

Leave a Reply

Your email address will not be published.