Thursday, August 16, 2012

How to sort ArrayList in java - List Sorting - Ascending Descending Order

How to sort ArrayList ascending descending order
Sorting ArrayList in Java is not difficult, by using Collections.sort() method you can sort ArrayList in ascending and descending order in Java. Collections.sort() method optionally accept a Comparator and if provided it uses Comparator's compare method to compare Objects stored in Collection to compare with each other, in case of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored in ArrayList doesn't implements Comparable than they can not be sorted using Collections.sort() method in Java.


Sorting ArrayList in Java – Code Example

ArrayList Sorting Example in Java - Ascending Descending Order Code
Here is a complete code example of How to sort ArrayList in Java, In this Sorting we have used Comparable method of String for sorting String on there natural order, You can also use Comparator in place of Comparable to sort String on any other order than natural ordering e.g. in reverse order by using Collections.reverseOrder() or in case insensitive order by using String.CASE_INSENSITIVE_COMPARATOR.

import java.util.ArrayList;
import java.util.Collections;
 * Java program to demonstrate How to sort ArrayList in Java in both ascending
 * and descending order by using core Java libraries.
 * @author Javin

public class CollectionTest {

    public static void main(String args[]) {
        //Creating and populating ArrayList in Java for Sorting
        ArrayList<String> unsortedList = new ArrayList<String>();
        System.err.println("unsorted ArrayList in Java : " + unsortedList);
        //Sorting ArrayList in ascending Order in Java
        System.out.println("Sorted ArrayList in Java - Ascending order : " + unsortedList);
        //Sorting ArrayList in descending order in Java
        Collections.sort(unsortedList, Collections.reverseOrder());
        System.err.println("Sorted ArrayList in Java - Descending order : " + unsortedList);
unsorted ArrayList in Java : [Java, C++, J2EE]
Sorted ArrayList in Java - Ascending order : [C++, J2EE, Java]
Sorted ArrayList in Java - Descending order : [Java, J2EE, C++]

That's all on How to Sort ArrayList in Java on both ascending and descending order. Just remember that Collections.sort() will sort the ArrayList in ascending order and if you provide reverse comparator it will sort the ArrayList in descending order in Java.

Other Java Collection tutorials:


  1. I have a List of Integers and a List of String, How can I sort them according to there natural order e.g. Sorting Integer List with numeric ascending order and Sorting String List with alphabetic order?

  2. This is indeed a good example to sort an ArrayList of String alphabetically, but this will also work in case of Integer, Date, or any other value type? I believe since it is using Comparable it will sort in the order defined there, as String Comparable sorts data alphabetically but Integer will be in increasing order and Date may be in chronological order. Correct me if I am wrong.


Java67 Headline Animator