Sorting Elements of Arrays and Wrapper Classes that Already Implements Comparable in Java
Last Updated :
09 Feb, 2023
Java provides the Comparable interface to sort objects using data members of the class. The Comparable interface contains only one method compareTo() that compares two objects to impose an order between them. It returns a negative integer, zero, or positive integer to indicate if the input object is less than, equal to, or greater than the current object. It is mainly used to sort the arrays or lists of custom objects.
Since all the Wrapper classes already implement Java Comparable interface, so it provides a default implementation of the compareTo(), and this is why Collections.sort() and Arrays.sort() functions can be used on these objects. Sorting elements of arrays and lists containing Wrapper classes as objects that already implement Comparable interface.
Illustration:
Input : {8 , 9 , 1 , 5 , 3 , 0}
Output : {0 , 1 , 3 , 5 , 8 , 9}
Input : {"Ankit", "Parul" , "Swati" , "Tarun", "Akshat"}
Output : {"Akshat" , "Ankit" , "Parul" , "Swati" , "Tarun"}
Implementation:
Example
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int [] a = { 8 , 9 , 1 , 5 , 3 , 0 };
System.out.println( "Before Sorting: "
+ Arrays.toString(a));
Arrays.sort(a);
System.out.println( "After Sorting: "
+ Arrays.toString(a));
String[] str = { "Ankit" , "Parul" , "Swati" , "Tarun" ,
"Akshat" };
System.out.println( "Before Sorting: "
+ Arrays.toString(str));
Arrays.sort(str);
System.out.println( "After Sorting: "
+ Arrays.toString(str));
List<String> lt = Arrays.asList( "Red" , "Blue" ,
"Green" , "Black" );
System.out.println( "Before Sorting: " + lt);
Collections.sort(lt);
System.out.println( "After Sorting:: " + lt);
}
}
|
Output
Before Sorting: [8, 9, 1, 5, 3, 0]
After Sorting: [0, 1, 3, 5, 8, 9]
Before Sorting: [Ankit, Parul, Swati, Tarun, Akshat]
After Sorting: [Akshat, Ankit, Parul, Swati, Tarun]
Before Sorting: [Red, Blue, Green, Black]
After Sorting:: [Black, Blue, Green, Red]
Now, if we want to sort a user-defined class into some specific order then we have to implement the Comparable interface which is present in java.lang package and provide the implementation of compareTo() method. The compareTo method also throws a NullPointerException or ClassCastException if the specified is null or if the type of the specified object prevents it from comparing to the object.
Implementation:
Example
Java
import java.util.*;
class Student implements Comparable<Student> {
private int rollno;
private String name;
private Double marks;
public Student( int rollno, String name, Double marks)
{
this .rollno = rollno;
this .name = name;
this .marks = marks;
}
public int compareTo(Student s)
{
return this .marks.compareTo(s.marks);
}
public String toString()
{
return "Student{"
+ "RollNo=" + rollno + ", Name='" + name + '\ ''
+ ", Marks=" + marks + '}' ;
}
}
class GFG {
public static void main(String args[])
{
ArrayList<Student> arr = new ArrayList<>();
arr.add( new Student( 1 , "Ankush" , 98.0 ));
arr.add( new Student( 2 , "Akshat" , 99.0 ));
arr.add( new Student( 3 , "Parul" , 87.0 ));
arr.add( new Student( 4 , "Tarun" , 78.0 ));
arr.add( new Student( 5 , "Swati" , 90.0 ));
for ( int i = 0 ; i < arr.size(); i++)
System.out.println(arr.get(i));
Collections.sort(arr);
System.out.println( "\nAfter Sorting :\n" );
for ( int i = 0 ; i < arr.size(); i++)
System.out.println(arr.get(i));
}
}
|
Output
Student{RollNo=1, Name='Ankush', Marks=98.0}
Student{RollNo=2, Name='Akshat', Marks=99.0}
Student{RollNo=3, Name='Parul', Marks=87.0}
Student{RollNo=4, Name='Tarun', Marks=78.0}
Student{RollNo=5, Name='Swati', Marks=90.0}
After Sorting :
Student{RollNo=4, Name='Tarun', Marks=78.0}
Student{RollNo=3, Name='Parul', Marks=87.0}
Student{RollNo=5, Name='Swati', Marks=90.0}
Student{RollNo=1, Name='Ankush', Marks=98.0}
Student{RollNo=2, Name='Akshat', Marks=99.0}
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...