Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

  • Difficulty Level : Easy
  • Last Updated : 24 Nov, 2020

 Sorting a string array in Lexicographical Order (Dictionary Order) using two approaches:

  1. By using any sorting technique to sort array elements.
  2. By using sort() function present in Arrays class in util package in java.

Example:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input : Harit Girish Gritav Lovenish Nikhil Harman 
Output: Girish Gritav Harit Harman Lovenish Nikhil

Input : Bob Alice
Output: Alice Bob 

Approach 1: Simple sorting technique.

In this approach, two strings are compared using compareToIgnoreCase() method of String class.



Functions used:

int compareToIgnoreCase(String);

Return value:

  1. 0 if the argument is a string lexicographically equal to this string.
  2. Less than 0 if the argument is a string lexicographically greater than this string.
  3. Greater than 0 if the argument is a string lexicographically less than this string.

A java program to sort an array of Strings in Lexicographical Order using sorting technique :

Java




// Java Program to Sort Elements in
// Lexicographical Order (Dictionary Order)
import java.io.*;
  
class GFG {
  
    // this method sort the string array lexicographically.
    public static void
    sortLexicographically(String strArr[])
    {
        for (int i = 0; i < strArr.length; i++) {
            for (int j = i + 1; j < strArr.length; j++) {
                if (strArr[i].compareToIgnoreCase(strArr[j])
                    > 0) {
                    String temp = strArr[i];
                    strArr[i] = strArr[j];
                    strArr[j] = temp;
                }
            }
        }
    }
  
    // this function prints the array passed as argument
    public static void printArray(String strArr[])
    {
        for (String string : strArr)
            System.out.print(string + " ");
        System.out.println();
    }
  
    public static void main(String[] args)
    {
        // Initializing String array.
        String stringArray[]
            = { "Harit",    "Girish", "Gritav",
                "Lovenish", "Nikhil", "Harman" };
  
        // sorting String array lexicographically.
        sortLexicographically(stringArray);
  
        printArray(stringArray);
    }
}
Output
Girish Gritav Harit Harman Lovenish Nikhil 

Time Complexity: O(n2), where n is the size of an array.

Approach 2: By using Arrays.sort()

In this approach, sort() method in a java.utils.Arrays class method is used.

Functions used:

Arrays.sort(stringArray, String.CASE_INSENSITIVE_ORDER);

// First Parameter : Name of Array
// Second Parameter: Special command to ignore case while sorting.

A java program to sort array of Strings in Lexicographical Order using Arrays.sort() method :

Java




// Java Program to Sort Elements in
// Lexicographical Order (Dictionary Order)
import java.io.*;
import java.util.Arrays;
  
class GFG {
    // this function prints the array passed as argument
    public static void printArray(String strArr[])
    {
        for (String string : strArr)
            System.out.print(string + " ");
        System.out.println();
    }
    public static void main(String[] args)
    {
        // Initializing String array.
        String stringArray[]
            = { "Harit",    "Girish", "Gritav",
                "Lovenish", "Nikhil", "Harman" };
  
        // sorting String array in Lexicographical Order.
        // Ingonring the case of string.
        Arrays.sort(stringArray,
                    String.CASE_INSENSITIVE_ORDER);
  
        // printing String array after sorting.
        printArray(stringArray);
    }
}
Output
Girish Gritav Harit Harman Lovenish Nikhil 

Time Complexity: O ( n log n )




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!