Java Arraylist sort() Function
What is an ArrayList?
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)
Definition of Parameters:
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.
Example using collection:
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]
Example Using the Naturalorder:
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]
Example Using ReverseOrder:
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
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment