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 :
- Using Arrays.sort() method
- 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:
- Java 8 – How to sort ArrayList elements ?
- Java 8 – Sorting List of primitive, String & Custom Objects
- Java 8 – Sorting ArrayList using sort() method of List
- Java 8 – How to sort LinkedList elements ?
- Java 8 – How to sort HashSet elements ?
- Java 8 – How to sort LinkedHashSet elements ?
- Java 8 – How to sort TreeSet in descending order ?
- Java 8 – How to sort List and Arrays with null values present ?
- Java 8 – How to sort Arrays in Ascending and Descending order ?
- Java 8 – Sorting list of objects on multiple fields (3-level attributes)
- Java 8 – How to Sort String[] arrays by its length in Ascending and Descending order ?
- Java 8 – How to Sort String List by its length in Ascending and Descending order ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Happy Coding !!
Happy Learning !!