Sorting Strings Alphabetically in Java

Sorting strings alphabetically is a common task in programming, often used in search engines, dictionaries, and data organization. In this article, we will explore different ways to sort an array of strings lexicographically in Java using Arrays.sort(), Collections.sort(), and custom comparators for case-insensitive and reverse order sorting.

Problem Statement

Given an array of strings, write a Java program to sort them lexicographically (alphabetically). Lexicographical ordering follows dictionary order, where strings are compared character by character.

Example

Approach to Solve the Problem

  1. Use Arrays.sort() – The simplest and most efficient method for sorting arrays in Java.
  2. Use Collections.sort() – If the data is stored in an ArrayList.
  3. Implement Custom Sorting – If we need case-insensitive or reverse sorting.

Java Program for Sorting Strings Alphabetically

1. Using Arrays.sort() (Recommended Approach)

import java.util.Arrays;

public class SortStrings {
    public static void main(String[] args) {
        String[] words = {"banana", "apple", "grape", "orange", "peach"};

        // Sorting the array alphabetically
        Arrays.sort(words);

        // Printing the sorted array
        System.out.println("Sorted Strings: " + Arrays.toString(words));
    }
}

2. Using Collections.sort() for List Sorting

import java.util.*;

public class SortStringsList {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>(Arrays.asList("banana", "apple", "grape", "orange", "peach"));

        // Sorting the list alphabetically
        Collections.sort(words);

        // Printing the sorted list
        System.out.println("Sorted Strings: " + words);
    }
}

3. Sorting Strings Case-Insensitive (Comparator Custom Sorting)

import java.util.*;

public class CaseInsensitiveSorting {
    public static void main(String[] args) {
        String[] words = {"Banana", "apple", "grape", "Orange", "peach"};

        // Sorting ignoring case
        Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);

        System.out.println("Case-Insensitive Sorted Strings: " + Arrays.toString(words));
    }
}

4. Sorting in Reverse Order

To sort strings in descending (Z to A) order, we can use Comparator.reverseOrder().

import java.util.Arrays;
import java.util.Collections;

public class ReverseSortStrings {
    public static void main(String[] args) {
        String[] words = {"banana", "apple", "grape", "orange", "peach"};

        // Sorting in reverse order
        Arrays.sort(words, Collections.reverseOrder());

        System.out.println("Reverse Sorted Strings: " + Arrays.toString(words));
    }
}

Conclusion

This article covered sorting an array of strings alphabetically using:

Arrays.sort() (Best for simple sorting)
Collections.sort() (Best for Lists)
Comparator for case-insensitive sorting
Reverse order sorting (Descending order)

These techniques are useful in search engines, databases, and natural language processing.

You can learn the Top 20 string array programs for interview preparation (Click here).

Java logical programs list


Java Basic Programs

Java String Programs

Java String Array Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Leave a Reply

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