Java Program to sort an array in alphabetical order


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

Updated on: 30-Jul-2019

597 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements