beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to sort ArrayList in Java

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we have shared the examples of sorting an String ArrayList and Integer ArrayList.

Also Read:

  1. Sort ArrayList in descending order
  2. Sort ArrayList of Objects using Comparable and Comparator

Example 1: Sorting of ArrayList<String>

Here we are sorting the ArrayList of String type. We are doing it by simply calling the Collections.sort(arraylist) method. The output List will be sorted alphabetically.

import java.util.*;
public class Details  {

	public static void main(String args[]){
	   ArrayList<String> listofcountries = new ArrayList<String>();
	   listofcountries.add("India");
	   listofcountries.add("US");
	   listofcountries.add("China");
	   listofcountries.add("Denmark");

	   /*Unsorted List*/
	   System.out.println("Before Sorting:");
	   for(String counter: listofcountries){
			System.out.println(counter);
		}

	   /* Sort statement*/
	   Collections.sort(listofcountries);

	   /* Sorted List*/
	   System.out.println("After Sorting:");
	   for(String counter: listofcountries){
			System.out.println(counter);
		}
	}
}

Output:

Before Sorting:
India
US
China
Denmark
After Sorting:
China
Denmark
India
US

Example 2: Sorting of ArrayList<Integer>

The same Collections.sort() method can be used for sorting the Integer ArrayList as well.

import java.util.*;
public class ArrayListOfInteger  {

	public static void main(String args[]){
	   ArrayList<Integer> arraylist = new ArrayList<Integer>();
	   arraylist.add(11);
	   arraylist.add(2);
	   arraylist.add(7);
	   arraylist.add(3);
	   /* ArrayList before the sorting*/
	   System.out.println("Before Sorting:");
	   for(int counter: arraylist){
			System.out.println(counter);
		}

	   /* Sorting of arraylist using Collections.sort*/
	   Collections.sort(arraylist);

	   /* ArrayList after sorting*/
	   System.out.println("After Sorting:");
	   for(int counter: arraylist){
			System.out.println(counter);
		}
	}
}

Output:

Before Sorting:
11
2
7
3
After Sorting:
2
3
7
11

Enjoyed this post? Try these related posts

  1. Check key & Value existence in Hashtable example – Java
  2. Java – Remove element from a specific index in LinkedList example
  3. Java ArrayList add(int index, E element) example
  4. Java ArrayList isEmpty() Method example
  5. Difference between ArrayList and Vector In java
  6. How to sort HashMap in Java by Keys and Values

Comments

  1. papajo says

    December 17, 2014 at 5:53 PM

    Hi, good article, but will Collections.sort eliminate duplicates in case the list contains any duplicates? If not can you show how to eliminate the duplicates?

    Reply
    • Chaitanya Singh says

      December 19, 2014 at 5:09 AM

      Refer this: https://beginnersbook.com/2014/10/how-to-remove-repeated-elements-from-arraylist/

      Reply
  2. Frank says

    July 6, 2015 at 3:22 PM

    For the main method it is supposed to be (String[] args) not (String args[])

    Reply
    • Chaitanya Singh says

      July 10, 2015 at 1:32 PM

      Hello Frank, Both are same.

      Reply
  3. Gopal says

    August 29, 2015 at 9:46 AM

    Great tutorial, even for beginners. Sorted my array immediately.

    Reply
  4. ni says

    July 10, 2017 at 3:23 PM

    One question, does .sort retain existing order? This is often relevant. If you have it sorted by one criteria, then sort it by another criteria, do values by the second criteria which are equal still show the initial sort criteria is retained, or is the resulting order random?

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap