- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Sort Array list in an Ascending Order
In this article, we will understand how to sort array list in an ascending order. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.
Below is a demonstration of the same −
Suppose our input is −
Input list: [java, coding, is, fun]
The desired output would be −
The sorted list is: [coding, fun, is, java]
Algorithm
Step 1 - START Step 2 - Declare an ArrayList namely input_list. Step 3 - Define the values. Step 4 - Use the function Collections.sort() to sort the list. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.*; public class Demo { public static void main(String args[]){ ArrayList<String> input_list = new ArrayList<String>(); input_list.add("java"); input_list.add("coding"); input_list.add("is"); input_list.add("fun"); System.out.println("The list is defined as: " + input_list); Collections.sort(input_list); System.out.println("The sorted list is: " + input_list); } }
Output
The list is defined as: [java, coding, is, fun] The sorted list is: [coding, fun, is, java]
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.*; public class Demo { static void sort(ArrayList<String> input_list){ Collections.sort(input_list); System.out.println("The sorted list is: " + input_list); } public static void main(String args[]){ ArrayList<String> input_list = new ArrayList<String>(); input_list.add("java"); input_list.add("coding"); input_list.add("is"); input_list.add("fun"); System.out.println("The list is defined as: " + input_list); sort(input_list); } }
Output
The list is defined as: [java, coding, is, fun] The sorted list is: [coding, fun, is, java]
- Related Articles
- C program to sort an array in an ascending order
- 8086 program to sort an integer array in ascending order
- C program to sort an array of ten elements in an ascending order
- Python program to sort the elements of an array in ascending order
- How to sort Java array elements in ascending order?
- Java program to sort words of sentence in ascending order
- How to sort an ArrayList in Ascending Order in Java
- How to sort an ArrayList in Java in ascending order?
- Java Program to sort an array in alphabetical order
- Program to sort a given linked list into ascending order in python
- C program to sort a given list of numbers in ascending order using Bubble sort
- Java Program to sort an array in case-insensitive order
- Java Program to sort an array in case-sensitive order
- How do you sort an array in C# in ascending order?
- 8085 Program to perform bubble sort in ascending order

Advertisements