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.

Leave a comment

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