How to sort an Array of Strings in Java
Last Updated :
16 Feb, 2024
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));
}
}
OutputModified arr[] :
[code 1="practice.geeksforgeeks.org," 2="www.geeksforgeeks.org" language=".geeksforgeeks.org,"][/code]
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);
}
}
OutputModified ArrayList :
[code 1="practice.geeksforgeeks.org," 2="www.geeksforgeeks.org" language=".geeksforgeeks.org,"][/code]
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);
}
}
}
OutputSorted 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
Outputapple
banana
grape
orange
Time Complexity: O(NlogN)
Space Complexity: O(1)
Similar Reads
Sort an Array in Java using Comparator A Comparator is an object that can be used to compare two objects and determine their order. We can use a Comparator to sort a list of objects in any order we can choose, not just in ascending order.Examples:Array(Ascending Order): Input: arr = (4, 2, 5, 1, 3) Output: [1, 2, 3, 4, 5] Array(Descendin
4 min read
C Program to Sort an array of names or strings Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings. C #include <stdio.h> #include <stdlib.h> #include <string.
2 min read
Sort given words as Array of Strings Given an array of strings Arr[]. The task is to sort them in lexicographic order. Examples: Input: Arr[] = {"sort", "this", "list"}Output: [list, sort, this] Input: Arr[] = {"sun", "earth", "mars", "mercury"}Output: [earth, mars, mercury, sun] Selection Sort Approach: The same problem can also be so
15+ min read
How to sort an array in a single loop? Given an array of size N, the task is to sort this array using a single loop.How the array is sorted usually? There are many ways by which the array can be sorted in ascending order, like: Selection SortBinary SortMerge SortRadix SortInsertion Sort, etc In any of these methods, more than 1 loops is
12 min read
Sort an Array of Strings in Lexicographical order Given an array of strings arr[] of size n, the task is to sort all the strings in lexicographical order. Examples:Input: arr[] = ["banana", "apple", "cherry"]Output: ["apple", "banana", "cherry"]Explanation: All strings are sorted alphabetically. "apple" comes before "banana", and "banana" before "c
11 min read