Java Program to sort an array in alphabetical order

Java 8Object Oriented ProgrammingProgramming

Let us first create a string array:

String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };

Now, use the Arrays.sort() method to sort an array in alphabetical order. Here, we have set the order to be case insensitive:

Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);

Example

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };
      System.out.println("Sort array in alphabetical order...");
      Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);
      for (int a = 0; a < strArr.length; a++) {
         System.out.println(strArr[a]);
      }
   }
}

Output

Sort array in alphabetical order...
p
q
r
s
v
y
raja
Published on 25-Apr-2019 16:08:15
Advertisements