In the Article, we will discuss about the ArrayList’s sort() Method in java. Arraylist is a part of collection framework in java. It contains some inbuilt function like sort function which can be found in the java.util.ArrayList Package. It implements the List interface of the collections framework.
Java ArrayList sort Function:
Arraylist contains several inbuilt functions that are used to perform several operations in of arraylist in Java. sort() function is also an inbuilt method of class ArrayList. sort() method is used to sorts the elements in an ArrayList according to the specified order.
Syntax:
arraylist_name.sort(Comparator c)
NoteThe Comparator in Java is an interface which is used for sorting the Arraylist in the specified manner. The Comparator is used to sort an ArrayList of User-defined objects.
Definition of Parameters:
arrayList_name This contains the name of the arraylist in which the functions have to be performed.
Comparator cThis helps to specifies the sort order of the ArrayList
Return Type :
The sort() method in Java does not return any value and does not have any return type. Although it only helps to change the order of the elements of an ArrayList.
import java.util.*;
public class Main{
public static void main(String[] args){
// Initializing the arraylist
ArrayList PrepInsta = new ArrayList<>();
// Adding Elements to the ArrayList
PrepInsta.add(20);
PrepInsta.add(10);
PrepInsta.add(80);
PrepInsta.add(50);
// Printing the ArrayList
System.out.println("ArrayList: " + PrepInsta);
// sort Function for true value
Collections.sort(PrepInsta);
// Printing the sorted ArrayList
System.out.println("Sorted order of the ArrayList : " + PrepInsta);
}
}
Output:
ArrayList: [20, 10, 80, 50]
Sorted order of the ArrayList : [10, 20, 50, 80]
Explanation:In the above code we had taken an integer ArrayList with some integer elements and printing the sorted ArrayList which is sorted using the collection framework of Java.
import java.util.*;
public class Main{
public static void main(String[] args){
// Initializing the arraylist
ArrayList PrepInsta = new ArrayList<>();
// Adding Elements to the ArrayList
PrepInsta.add(20);
PrepInsta.add(10);
PrepInsta.add(80);
PrepInsta.add(50);
// Printing the ArrayList
System.out.println("ArrayList: " + PrepInsta);
// sort Function for true value
PrepInsta.sort(Comparator.naturalOrder());
// Printing the sorted ArrayList
System.out.println("Sorted order of the ArrayList : " + PrepInsta);
}
}
Output:
ArrayList: [20, 10, 80, 50]
Sorted order of the ArrayList : [10, 20, 50, 80]
Explanation:In the above Example. we added 4 elements to an ArrayList of Integer type and we are printing the sorted ArrayList in their natural order using the sort() method of Java.
import java.util.*;
public class Main{
public static void main(String[] args){
// Initializing the arraylist
ArrayList PrepInsta = new ArrayList<>();
// Adding Elements to the ArrayList
PrepInsta.add(20);
PrepInsta.add(10);
PrepInsta.add(80);
PrepInsta.add(50);
// Printing the ArrayList
System.out.println("ArrayList: " + PrepInsta);
// sort Function in Reverse Order
PrepInsta.sort(Comparator.reverseOrder());
// Printing the sorted ArrayList
System.out.println("Sorted order of the ArrayList : " + PrepInsta);
}
}
Output:
ArrayList: [20, 10, 80, 50]
Sorted order of the ArrayList : [80, 50, 20, 10]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment