How to Sort ArrayList in Java | Collections.sort() Example

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how to sort ArrayList in java using default sorting order.
To Sort an ArrayList in Java we have an utility method Collections.sort() from Collections class. The sort(List<>) method takes the list object and it sort the list according to the natural order by default it is asscending order.
No matter which type of objets are there in the list, whether String type or Numarical types.
Creating ArrayList String type:
ArrayList<String> fruitsList = new ArrayList();
fruitsList.add("Banana");
fruitsList.add("Apple");
fruitsList.add("Grapes");
fruitsList.add("Mango");
I am going to sort the above ArrayList in default sorting order.
System.out.println("Before Sorting");
System.out.println("--------------");
fruitsList.forEach((fruit)->System.out.println(fruit));
System.out.println("After Sorting");
System.out.println("--------------");
Collections.sort(fruitsList);
fruitsList.forEach((fruit)->System.out.println(fruit));
Output:
Before Sorting
--------------
Banana
Apple
Grapes
Mango
After Sorting
--------------
Apple
Banana
Grapes
Mango
Creating ArrayList Double type:
Creating Double type ArrayList.
ArrayList<Double> data = new ArrayList();
data.add(0.5);
data.add(0.54);
data.add(0.92);
data.add(0.12);
data.add(0.62);
Sorting Double type ArrayList in default sorting order.
System.out.println("Before Sorting");
System.out.println("--------------");
data.forEach((item)->System.out.println(item));
System.out.println("After Sorting");
System.out.println("--------------");
Collections.sort(data);
data.forEach((item)->System.out.println(item));

Output :
Before Sorting
--------------
0.5
0.54
0.92
0.12
0.62
After Sorting
--------------
0.12
0.5
0.54
0.62
0.92
Happy Learning 🙂

How to Sort ArrayList in Java Descending Order
Java Program for String Sorting Example
How to Shuffle an Array using Java
PHP Array Sort Techniques Tutorials
What are the different ways to Sort Objects in Python ?
ArrayList in Java
How to add elements to ArrayList in Java
Difference between ArrayList vs Vector in Java
2 ways to Create ArrayList in Java
5 Ways to Iterate ArrayList in Java
Java how to convert ArrayList to Array Example
Bubble Sort In Java
Java Insertion Sort
Selection Sort In Java
How to Sort an Array in Parallel in Java 8

How to Sort ArrayList in Java Descending Order
Java Program for String Sorting Example
How to Shuffle an Array using Java
PHP Array Sort Techniques Tutorials
What are the different ways to Sort Objects in Python ?
ArrayList in Java
How to add elements to ArrayList in Java
Difference between ArrayList vs Vector in Java
2 ways to Create ArrayList in Java
5 Ways to Iterate ArrayList in Java
Java how to convert ArrayList to Array Example
Bubble Sort In Java
Java Insertion Sort
Selection Sort In Java
How to Sort an Array in Parallel in Java 8
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String