Sort ArrayList in java

In the previous post, we have seen how to remove an element at a specified index. In this post, we will see how to sort arraylist in java.

We use the collections class to sort the arraylist.

Syntax:

Collections.sort(arraylist)

Example for sorting String:

We will see how to sort the string.

Output:

List of fruits before sorting : [Orange, Mango, Apple, Grapes] List of fruits after sorting : [Apple, Grapes, Mango, Orange]

 

Example for sorting the integer:

We will see how to sort the Integer values.

Output:

List of integer before sorting : [4, 2, 6, 1] List of integer after sorting : [1, 2, 4, 6]

Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

 

Print Friendly, PDF & Email
Remove an element at a particular index from the array list
Sort the ArrayList in descending order
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *