Open In App

How to sort an Array of Strings in Java

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Array Of Strings

To sort an array of strings in Java, we can use Arrays.sort() function.

Java




// A sample Java program to
// sort an array of strings
// in ascending and descending
// orders using Arrays.sort().
 
import java.util.Arrays;
import java.util.Collections;
 
// Driver Class
public class SortExample {
      // main method 
      public static void main(String[] args)
    {
        String arr[] = { "practice.geeksforgeeks.org",
                         "www.geeksforgeeks.org",
                         "code.geeksforgeeks.org" };
 
        // Sorts arr[] in ascending order
        Arrays.sort(arr);
        System.out.printf("Modified arr[] : \n%s\n\n",
                          Arrays.toString(arr));
 
        // Sorts arr[] in descending order
        Arrays.sort(arr, Collections.reverseOrder());
 
        System.out.printf("Modified arr[] : \n%s\n\n",
                          Arrays.toString(arr));
    }
}


Output

Modified arr[] : 


Modified arr[] : 
[www.geeksforgeeks.org, practice.geeksforgeeks.org, code.geeksforgeeks.org]


ArrayList Of Strings

We have an ArrayList to sort, we can use Collections.sort()

Java




// A sample Java program to sort
// an arrayList of strings
// in ascending and descending
// orders using Collections.sort().
 
import java.util.ArrayList;
import java.util.Collections;
 
// Driver Class
public class SortExample {
    // main method
    public static void main(String[] args)
    {
 
        ArrayList<String> al = new ArrayList<String>();
        al.add("practice.geeksforgeeks.org");
        al.add("www.geeksforgeeks.org");
        al.add("code.geeksforgeeks.org");
 
        // Sorts ArrayList in ascending order
        Collections.sort(al);
        System.out.println("Modified ArrayList : \n" + al);
 
        // Sorts arr[] in descending order
        Collections.sort(al, Collections.reverseOrder());
 
        System.out.println("Modified ArrayList : \n" + al);
    }
}


Output

Modified ArrayList : 

Modified ArrayList : 
[www.geeksforgeeks.org, practice.geeksforgeeks.org, code.geeksforgeeks.org]

Sort an array of strings using a custom comparator

Java




// Java Program to Sort an array of strings
// Using a custom comparator
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
 
// Driver Class
class SortStrings {
    // main method
    public static void main(String[] args)
    {
        String[] fruits = { "apple", "orange", "banana",
                            "pear", "kiwi" };
 
        // Sort the array of strings by length
        Arrays.sort(fruits,
                    Comparator.comparing(String::length));
 
        // Print statement
        System.out.println("Sorted fruits by length:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}


Output

Sorted fruits by length:
pear
kiwi
apple
orange
banana

Java 8 Streams to sort an array of strings

In this method, the Arrays.stream(arrayOfStrings) creates a stream of strings from the array. The sorted() operation is then applied to the stream, which sorts the elements in their natural order (lexicographical order for strings). Finally, the forEach method is used to print each element of the sorted stream. This method leverages the functional programming features introduced in Java 8 with the Stream API.

Below is the Implementation of the above approach:

Java




// Java program for the above approach
import java.util.Arrays;
import java.util.stream.Stream;
 
public class StringArraySorting {
    public static void main(String[] args)
    {
        // Sample array of strings
        String[] arrayOfStrings
            = { "banana", "apple", "orange", "grape" };
 
        // Using Java 8 Streams to sort the array
        Arrays
            .stream(arrayOfStrings) // Creating a stream
                                    // from the array
            .sorted() // Sorting the stream of strings
            .forEach(System.out::println); // Printing each
                                           // sorted element
    }
}
 
// This code is contributed by Susobhan Akhuli


Output

apple
banana
grape
orange

Time Complexity: O(NlogN)
Space Complexity: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads