Sorting Strings in ArrayList – Ascending and Descending order

We have seen sorting of integer arraylist in ascending and descending order, in this post we will see how to sort string arraylist in ascending and descending order. Also we will see sorting for Uppercase,Lowercase and combination of both.

  1. Sorting  of  Lowercase Strings in ascending and descending order
    
    package ArrayList;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class ArrayListSortString {
    
    public static void main(String[] args) {
     // TODO Auto-generated method stub
     List<String> lowerCaseStringListOne = new ArrayList<>();
     lowerCaseStringListOne.add("apple");
     lowerCaseStringListOne.add("cat");
     lowerCaseStringListOne.add("banana");
    
     System.out.println("List of Lowercase Strings before sort " +lowerCaseStringListOne);
    
     Collections.sort(lowerCaseStringListOne);
    
     System.out.println("List of Lowercase Strings after sort " +lowerCaseStringListOne);
    
     System.out.println("---------------------------------------------------------------");
     List<String> lowerCaseStringListTwo = new ArrayList<>();
     lowerCaseStringListTwo.add("apple");
     lowerCaseStringListTwo.add("cat");
     lowerCaseStringListTwo.add("banana");
    
     System.out.println("List of Lowercase Strings before reverse sort " +lowerCaseStringListTwo);
    
     Collections.sort(lowerCaseStringListTwo, Collections.reverseOrder());
    
     System.out.println("List of Lowercase Strings after reverse sort " +lowerCaseStringListTwo);
     }
    
    }
    
          

    Output will be like this

     
    
    List of Lowercase Strings before sort [apple, cat, banana]
    List of Lowercase Strings after sort [apple, banana, cat]
    ---------------------------------------------------------------
    List of Lowercase Strings before reverse sort [apple, cat, banana]
    List of Lowercase Strings after reverse sort [cat, banana, apple]
    
    
  2.      Sorting of Uppercase Strings in ascending and descending order
    package ArrayList;
    import java.util.ArrayList;
    import java.util.Collections;
    
    import java.util.List;
    
    public class ArrayListSortString {
    
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<String> upperCaseStringListOne = new ArrayList<>();
    upperCaseStringListOne.add("APPLE");
    upperCaseStringListOne.add("CAT");
    upperCaseStringListOne.add("BANANA");
    
    System.out.println("List of Uppercase Strings before sort " +upperCaseStringListOne);
    
    Collections.sort(upperCaseStringListOne);
    
    System.out.println("List of Uppercase Strings after sort " +upperCaseStringListOne);
    
    System.out.println("---------------------------------------------------------------");
    List<String> upperCaseStringListTwo = new ArrayList<>();
    upperCaseStringListTwo.add("APPLE");
    upperCaseStringListTwo.add("CAT");
    upperCaseStringListTwo.add("BANANA");
    
    System.out.println("List of Uppercase Strings before reverse sort " +upperCaseStringListTwo);
    
    Collections.sort(upperCaseStringListTwo, Collections.reverseOrder());
    
    System.out.println("List of Uppercase Strings after reverse sort " +upperCaseStringListTwo);
    }
    
    }
    
    

    Output will be like this

    
    List of Uppercase Strings before sort [APPLE, CAT, BANANA]
    List of Uppercase Strings after sort [APPLE, BANANA, CAT]
    ---------------------------------------------------------------
    List of Uppercase Strings before reverse sort [APPLE, CAT, BANANA]
    List of Uppercase Strings after reverse sort [CAT, BANANA, APPLE]
    
    

    3.  Sorting  of  combination of lowercase and uppercase Strings in ascending and descending order

    
    package ArrayList;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class ArrayListSortStrings {
    
    public static void main(String[] args) {
    
    List<String> combinationStringOne = new ArrayList<>();
    //Ascii Value //Rank
    combinationStringOne.add("apple"); //(a)->97, (p)->112 7
    combinationStringOne.add("APPLE"); //(A)->65, (P)->80 1
    combinationStringOne.add("Apple"); //(A)->65, (p)->112 2
    combinationStringOne.add("cat"); //(c)->99, (a)->97 9
    combinationStringOne.add("CAT"); //(C)->67, (A)->65 5
    combinationStringOne.add("Cat"); //(C)->67, (a)->97 6
    combinationStringOne.add("banana"); //(b)->98, (a)->97 8
    combinationStringOne.add("BANANA"); //(B)->66, (A)->65 3
    combinationStringOne.add("Banana"); //(B)->66, (a)->97 4
    
    // So internally it will check for its ascii value and sort based on lowest ascii value,
    //if 2 strings starts with same character (eg, Apple and APPLE) then it will check for second character
    //ascii value and select based on tat value
    
    System.out.println("List of Combination of Strings before sort " +combinationStringOne);
    
    Collections.sort(combinationStringOne); //1
    
    System.out.println("List of Combination of Strings after sort " +combinationStringOne);
    System.out.println("------------------------------------------------------------");
    
    List<String> combinationStringTwo = new ArrayList<>();
    //Ascii Value //Rank
    combinationStringTwo.add("apple"); //(a)->97, (p)->112 7
    combinationStringTwo.add("APPLE"); //(A)->65, (P)->80 1
    combinationStringTwo.add("Apple"); //(A)->65, (p)->112 2
    combinationStringTwo.add("cat"); //(c)->99, (a)->97 9
    combinationStringTwo.add("CAT"); //(C)->67, (A)->65 5
    combinationStringTwo.add("Cat"); //(C)->67, (a)->97 6
    combinationStringTwo.add("banana"); //(b)->98, (a)->97 8
    combinationStringTwo.add("BANANA"); //(B)->66, (A)->65 3
    combinationStringTwo.add("Banana"); //(B)->66, (a)->97 4
    
    System.out.println("List of Combination of Strings before reverse sort " +combinationStringTwo);
    Collections.sort(combinationStringTwo,Collections.reverseOrder()); //2
    System.out.println("List of Combination of Strings after reverse sort " +combinationStringTwo);
    
    System.out.println("------------------------------------------------------------");
    // case insensitive order sort
    
    List<String> combinationStringThree = new ArrayList<>();
    
    combinationStringThree.add("apple");
    combinationStringThree.add("APPLE");
    combinationStringThree.add("Apple");
    combinationStringThree.add("cat");
    combinationStringThree.add("CAT");
    combinationStringThree.add("Cat");
    combinationStringThree.add("banana");
    combinationStringThree.add("BANANA");
    combinationStringThree.add("Banana");
    
    System.out.println("List of Combination of Strings before case incensitive sort " +combinationStringThree);
    
    Collections.sort(combinationStringThree, String.CASE_INSENSITIVE_ORDER);
    
    // CASE_INSENSITIVE_ORDER will first consider the lowercase values
    System.out.println("List of Combination of Strings after case incensitive sort " +combinationStringThree);
    System.out.println("------------------------------------------------------------");
    // case insensitive sort with reverse order
    
    List<String> combinationStringFour = new ArrayList<>();
    
    combinationStringFour.add("apple");
    combinationStringFour.add("APPLE");
    combinationStringFour.add("Apple");
    combinationStringFour.add("cat");
    combinationStringFour.add("CAT");
    combinationStringFour.add("Cat");
    combinationStringFour.add("banana");
    combinationStringFour.add("BANANA");
    combinationStringFour.add("Banana");
    
    System.out.println("List of Combination of Strings before case incensitive and reverse sort " +combinationStringFour);
    
    Collections.sort(combinationStringFour, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));
    
    // CASE_INSENSITIVE_ORDER will first consider the lowercase values
    System.out.println("List of Combination of Strings after case incensitive and reverse sort " +combinationStringFour);
    
    }
    }
    
    

    The output will be like this

    
    List of Combination of Strings before sort [apple, APPLE, Apple, cat, CAT, Cat, banana, BANANA, Banana]
    List of Combination of Strings after sort [APPLE, Apple, BANANA, Banana, CAT, Cat, apple, banana, cat]
    ------------------------------------------------------------
    List of Combination of Strings before reverse sort [apple, APPLE, Apple, cat, CAT, Cat, banana, BANANA, Banana]
    List of Combination of Strings after reverse sort [cat, banana, apple, Cat, CAT, Banana, BANANA, Apple, APPLE]
    ------------------------------------------------------------
    List of Combination of Strings before case incensitive sort [apple, APPLE, Apple, cat, CAT, Cat, banana, BANANA, Banana]
    List of Combination of Strings after case incensitive sort [apple, APPLE, Apple, banana, BANANA, Banana, cat, CAT, Cat]
    ------------------------------------------------------------
    List of Combination of Strings before case incensitive and reverse sort [apple, APPLE, Apple, cat, CAT, Cat, banana, BANANA, Banana]
    List of Combination of Strings after case incensitive and reverse sort [cat, CAT, Cat, banana, BANANA, Banana, apple, APPLE, Apple]
    
    

    We have seen sorting of strings in arraylist, but how userdefined classes will be sorted in List.

How to Sort Integer ArrayList in Java – Ascending and Descending order

We have seen arraylist  is an Unordered and without sorted one by its default implementation. In this post we will see How to Sort ArrayList in Java. To Sort arraylist we have method called sort in Collections class. By default this method will sort in ascending order.

How to Sort ArrayList in Java

How to Sort ArrayList in Java

How to sort Arraylist in Java – in Reverse Direction

How to Sort ArrayList in Java

How to Sort ArrayList in Java

 

Now we will see the example of How to Sort ArrayList in Java which hold Integer Values.


package ArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrayListSortInteger {

public static void main(String[] args) {
 List<Integer> integersList = new ArrayList<>();
 integersList.add(1);
 integersList.add(45);
 integersList.add(3);

 System.out.println("Integer List Without Sort " +integersList);//1,45,3

 // Collections.sort() --> sort the list in ascending order
 Collections.sort(integersList);

 System.out.println("Integer List With Sort " +integersList);//1,3,45

 //// Collections.sort() --> sort the list in descending (reverse) order
 Collections.sort(integersList, Collections.reverseOrder());

 System.out.println("Integer List With Sort in Reverse Order "+integersList);//45,3,1
 }
}

Output will be like this:


Integer List Without Sort [1, 45, 3]
Integer List With Sort [1, 3, 45]
Integer List With Sort in Reverse Order [45, 3, 1]

We have seen sorting of Integer arraylist in ascending and descending order. But How string will be sorted in list. Also How String with Uppercase and with lowercase will be sorted in list? Also How String will be sorted in reverse order.? Also How user defined class will be sorted? We will get answers to all these questions in our next tutorial.

If you have any questions on this tutorial, please post your questions in the comment box. Also If you like this tutorial please share it with your friends. Also To get regular updates from learnfromexample please subscribe to our website.

Related Tutorial Link Which You might Like:

  1. How to Convert ArrayList to Array in Java
  2. How to find the count of an ArrayList
  3. How to sort User Defined Objects in Arraylist