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
How to sort an ArrayList in Ascending Order in Java Given an unsorted ArrayList, the task is to sort this ArrayList in ascending order in Java. Examples: Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Output: Sorted ArrayList: [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks] Input: Unsorted ArrayList: [Gee
2 min read
String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array
5 min read
Java Program to Sort an ArrayList ArrayList is the class provided in the Collection framework. In Java, the collection framework is defined in java.util package. ArrayList is used to dynamically stores the elements. It is more flexible than an array because there is no size limit in ArrayList. ArrayList stores the data in an unorder
6 min read
How to sort an ArrayList in Descending Order in Java Given an unsorted ArrayList, the task is to sort this ArrayList in descending order in Java. Examples: Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Output: Sorted ArrayList: [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal] Input: Unsorted ArrayList: [Ge
2 min read
How to Sort an Arrays of Object using Arrays.sort() To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort(). Sorting an Array of ObjectWith Array of Object what it really means is an array st
3 min read