Java 8 – How to Sort String[] Arrays by its length ?

In this article, we will discuss how to sort String[] Arrays by its length in Ascending-order and Descending-order in Java 1.8 version

Sorting String[] Arrays by its length in Java 8 :

  1. Using Arrays.sort() method
  2. Using Arrays.stream.sorted() method

1. Using Arrays.sort() method :

  • Arrays.sort() method accepts 2 input-arguments where,
    • 1st argument is the actual String[] Arrays to be sorted
    • 2nd argument is the Comparator for sorting
  • For Sorting, pass Comparator as either of the below
    • Lambda expression or
    • Method References
  • For Ascending-order sorting, use any of the below Comparator
    • Lambda expression 1 – (str1, str2) -> str1.length() str2.length()
    • Lambda expression 2 – (str1, str2) -> Integer.compare(str1.length(), str2.length())
    • Method References – Comparator.comparingInt(String::length)
  • For Descending-order sorting, use any of the below Comparator
    • Lambda expression 1 – (str1, str2) -> str2.length() str1.length()
    • Lambda expression 2 – (str1, str2) -> Integer.compare(str2.length(), str1.length())
    • Method References – Comparator.comparingInt(String::length).reversed()
  • Print both ascending-order and descending-order sorted String[] Arrays in accordance with its String length to the console

SortingStringArraysByItsLengthUsingJava8ArraysSortMethod.java

package in.bench.resources.sorting.string.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class SortingStringArraysByItsLengthUsingJava8ArraysSortMethod {

	public static void main(String[] args) {

		// 1. names with different length
		String[] names = new String[] {
				"Bond",
				"James",
				"Einstein",
				"Alice",
				"Whitman",
				"Bob",
				"Binny",
				"Spider",
				"Lee",
				"Anderson"
		};


		// 1.1 print to console
		System.out.println("Original String[] Arrays :- \n" 
				+ Arrays.toString(names) + "\n");



		// 2. sorting String[] Arrays in Ascending-order
		Arrays.sort(names, Comparator.comparingInt(String::length));


		// 2.1 print ascending-order sorted Strings by its Length
		System.out.println("\nAscending-order Sorted String[] Arrays by its Length :- \n" 
				+ Arrays.toString(names) + "\n");



		// 3. sorting String[] Arrays in Descending-order
		Arrays.sort(names, (str1, str2) -> Integer.compare(str2.length(), str1.length()));


		// 3.1 print descending-order sorted Strings by its Length
		System.out.print("\nDescending-order Sorted String[] Arrays by its Length :- \n" 
				+ Arrays.toString(names));
	}
}

Output:

Original String[] Arrays :- 
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Lee, Anderson]


Ascending-order Sorted String[] Arrays by its Length :- 
[Bob, Lee, Bond, James, Alice, Binny, Spider, Whitman, Einstein, Anderson]


Descending-order Sorted String[] Arrays by its Length :- 
[Einstein, Anderson, Whitman, Spider, James, Alice, Binny, Bond, Bob, Lee]

2. Using Arrays.stream.sorted() method :

  • There is a String[] Arrays with different length in random-order which needs to be sorted according to String length
  • First get the stream from String[] Arrays using either of the below ways,
    • Arrays.stream() or
    • Stream.of()
  • Arrays.stream().sorted() method accepts Comparator as method-argument, pass either of the below
    • Lambda expression or
    • Method References
  • For Ascending-order sorting, use any of the below Comparator
    • Lambda expression 1 – (str1, str2) -> str1.length() str2.length()
    • Lambda expression 2 – (str1, str2) -> Integer.compare(str1.length(), str2.length())
    • Method References – Comparator.comparingInt(String::length)
  • For Descending-order sorting, use any of the below Comparator
    • Lambda expression 1 – (str1, str2) -> str2.length() str1.length()
    • Lambda expression 2 – (str1, str2) -> Integer.compare(str2.length(), str1.length())
    • Method References – Comparator.comparingInt(String::length).reversed()
  • Print both ascending-order and descending-order sorted String[] Arrays in accordance with its String length to the console

SortingStringArraysByItsLengthUsingJava8StreamSortedMethod.java

package in.bench.resources.sorting.string.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class SortingStringArraysByItsLengthUsingJava8StreamSortedMethod {

	public static void main(String[] args) {

		// 1. names with different length
		String[] names = new String[] {
				"Bond",
				"James",
				"Einstein",
				"Alice",
				"Whitman",
				"Bob",
				"Binny",
				"Spider",
				"Lee",
				"Anderson"
		};


		// 1.1 print to console
		System.out.println("Original String[] Arrays :- \n" 
				+ Arrays.toString(names) + "\n");



		// 2. sorting String[] Arrays in Ascending-order
		names = Arrays
				.stream(names)
				.sorted((str1, str2) -> str1.length() - str2.length())
				.toArray(String[]::new);


		// 2.1 print ascending-order sorted Strings by its Length
		System.out.println("\nAscending-order Sorted String[] Arrays by its Length :- \n" 
				+ Arrays.toString(names) + "\n");



		// 3. sorting String[] Arrays in Descending-order
		names = Arrays
				.stream(names)
				.sorted(Comparator.comparingInt(String::length).reversed())
				.toArray(String[]::new);


		// 3.1 print descending-order sorted Strings by its Length
		System.out.print("\nDescending-order Sorted String[] Arrays by its Length :- \n" 
				+ Arrays.toString(names));
	}
}

Output:

Original String[] Arrays :- 
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Lee, Anderson]


Ascending-order Sorted String[] Arrays by its Length :- 
[Bob, Lee, Bond, James, Alice, Binny, Spider, Whitman, Einstein, Anderson]


Descending-order Sorted String[] Arrays by its Length :- 
[Einstein, Anderson, Whitman, Spider, James, Alice, Binny, Bond, Bob, Lee]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java – How to Sort String List by its length ?
Java - How to Sort String[] Arrays by its length ?