How to Sort a String Array Lexicographically in Java

How to Sort a String Array Lexicographically in Java | Lexicographically means in dictionary order, we will be sorting the string array in the dictionary order.

For example:-
String array = [“Hi”, “This”, “is”, “Java”, ”Program”]
Output:- Hi, is, Java, Program, This

We use two different methods to sort the array in dictionary order.
1) Using a sorting technique
2) Using sort() method

How To Sort Strings Alphabetically In Java

Step-1: Create a method named sortLexicographical() which takes one string array as a parameter.
Step-2: Iterate through the string and compare the strings by using for loops.
Step-3: Then if the first string is greater than the second string swap them here and ignore the case of the strings.
Step-4: In the main method call the sortLexicographical() methods.

import java.util.Arrays;

public class Main {

   public static void sortLexicographically(String str[]) {
      for (int i = 0; i < str.length; i++) {
         for (int j = i + 1; j < str.length; j++) {
            if (str[i].compareToIgnoreCase(str[j]) > 0) {
               String temp = str[i];
               str[i] = str[j];
               str[j] = temp;
            }
         }
      }
   }

   public static void main(String[] args) {
      String stringArr[] = 
        {"Zen", "John", "Harry", "Albert", "Nikhi", "Harman"};
      System.out.println("String array: \n" 
         + Arrays.toString(stringArr));
      
      sortLexicographically(stringArr);
      System.out.println("After sorting Lexicographically: \n" 
          + Arrays.toString(stringArr));
   }
}

Output:-

String array:
[Zen, John, Harry, Albert, Nikhi, Harman]
After sorting Lexicographically:
[Albert, Harman, Harry, John, Nikhi, Zen]

To display the string array we have used the toString() method of the Java arrays class which is used to convert an array to a string.

Sort a String Array Lexicographically in Java

In the above program, we have defined a custom method to sort a string array lexicographically in Java. But instead of defining our own method, we can use the sort() method of the Arrays class.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      String stringArr[] = 
        {"Zen", "John", "Harry", "Albert", "Nikhi", "Harman"};
      System.out.println("String array: \n" 
         + Arrays.toString(stringArr));
      
      Arrays.sort(stringArr);
      // Or,
      // Arrays.sort(stringArr, String.CASE_INSENSITIVE_ORDER);
      
      System.out.println("After sorting Lexicographically: \n" 
          + Arrays.toString(stringArr));
   }
}

Output:-

String array:
[Zen, John, Harry, Albert, Nikhi, Harman]
After sorting Lexicographically:
[Albert, Harman, Harry, John, Nikhi, Zen]

By default Arrays.sort() method sorts the String array lexicographically. The CASE_INSENSITIVE_ORDER is a comparator defined in the string class. We can also pass a comparator as the second parameter in the Arrays.sort() method.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.