How to sort an Array of Strings in Java Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Array Of StringsTo 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 StringsWe 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 bananaJava 8 Streams to sort an array of stringsIn 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) Comment More infoAdvertise with us Next Article How to sort an Array of Strings in Java kartik Follow Improve Article Tags : Strings Java Sorting DSA Arrays Java-Arrays Java-ArrayList Python numpy-Random +4 More Practice Tags : ArraysJavaSortingStrings 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 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 Program to sort an array of strings using Selection Sort Given an array of strings, sort the array using Selection Sort. Examples: Input : paper true soap floppy flower Output : floppy, flower, paper, soap, true Prerequisite : Selection Sort. C++ // C++ program to implement selection sort for // array of strings. #include <bits/stdc++.h> #include 7 min read Like