In this article, we will discuss how to sort String List by its length in Ascending-order and Descending-order in Java 1.8 version
Sorting String List by its length in Java 8 :
- Using Collections.sort() method
- Using Stream.sorted() method
- Using List.sort() method
1. Using Collections.sort() method :
- Collections.sort() method accepts 2 input-arguments where,
- 1st argument is the actual String List 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 List in accordance with its String length to the console
SortingStringListByItsLengthUsingJava8CollectionsSortMethod.java
package in.bench.resources.sorting.string.list;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortingStringListByItsLengthUsingJava8CollectionsSortMethod {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"James",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Binny",
"Spider",
"Lee",
"Anderson"
);
// 1.1 print to console
System.out.println("Original String List :- \n" + names + "\n");
// 2. sorting String List in Ascending-order
Collections.sort(names, Comparator.comparingInt(String::length));
// 2.1 print ascending-order sorted Strings by its Length
System.out.println("\nAscending-order Sorted String List "
+ "by its Length :- \n" + names + "\n");
// 3. sorting String List in Descending-order
Collections.sort(names, Comparator.comparingInt(String::length).reversed());
// 3.1 print descending-order sorted Strings by its Length
System.out.print("\nDescending-order Sorted String List "
+ "by its Length :- \n" + names);
}
}
Output:
Original String List :-
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Lee, Anderson]
Ascending-order Sorted String List by its Length :-
[Bob, Lee, Bond, James, Alice, Binny, Spider, Whitman, Einstein, Anderson]
Descending-order Sorted String List by its Length :-
[Einstein, Anderson, Whitman, Spider, James, Alice, Binny, Bond, Bob, Lee]
2. Using Stream.sorted() method :
- There is a String List with different length in random-order which needs to be sorted according to String length
- Get the stream from List using List.stream() method
- 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 List in accordance with its String length to the console
SortingStringListByItsLengthUsingJava8StreamSortedMethod.java
package in.bench.resources.sorting.string.list;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SortingStringListByItsLengthUsingJava8StreamSortedMethod {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"James",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Binny",
"Spider",
"Lee",
"Anderson"
);
// 1.1 print to console
System.out.println("Original String List :- \n" + names + "\n");
// 2. sorting String List in Ascending-order
List<String> ascSortedList = names
.stream()
.sorted((str1, str2) -> str1.length() - str2.length())
.collect(Collectors.toList());
// 2.1 print ascending-order sorted Strings by its Length
System.out.println("\nAscending-order Sorted String[] Arrays "
+ "by its Length :- \n" + ascSortedList + "\n");
// 3. sorting String List in Descending-order
List<String> descSortedList = names
.stream()
.sorted((str1, str2) -> str2.length() - str1.length())
.collect(Collectors.toList());
// 3.1 print descending-order sorted Strings by its Length
System.out.print("\nDescending-order Sorted String[] Arrays "
+ "by its Length :- \n" + descSortedList);
}
}
Output:
Original String List :-
[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]
3. Using List.sort() method :
- There is a String List with different length in random-order which needs to be sorted according to String length
- List.sort() 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 List in accordance with its String length to the console
SortingStringListByItsLengthUsingJava8ListSortMethod.java
package in.bench.resources.sorting.string.list;
import java.util.Arrays;
import java.util.List;
public class SortingStringListByItsLengthUsingJava8ListSortMethod {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"James",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Binny",
"Spider",
"Lee",
"Anderson"
);
// 1.1 print to console
System.out.println("Original String List :- \n" + names + "\n");
// 2. sorting String List in Ascending-order
names.sort((str1, str2) -> Integer.compare(str1.length(), str2.length()));
// 2.1 print ascending-order sorted Strings by its Length
System.out.println("\nAscending-order Sorted String List "
+ "by its Length :- \n" + names + "\n");
// 3. sorting String List in Descending-order
names.sort((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 List "
+ "by its Length :- \n" + names);
}
}
Output:
Original String List :-
[Bond, James, Einstein, Alice, Whitman, Bob, Binny, Spider, Lee, Anderson]
Ascending-order Sorted String List by its Length :-
[Bob, Lee, Bond, James, Alice, Binny, Spider, Whitman, Einstein, Anderson]
Descending-order Sorted String List 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/Descending order ?
- Java 8 – How to Sort String List by its length in Ascending/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/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Collections.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 !!