In this article, we will discuss how to sort String List by its length in Ascending-order and Descending-order
Sorting String List by its length:
- Using TreeSet (inter-conversion constructor)
- Using Collections.sort() method
1. Using TreeSet :
- There is a String List with different length in random-order which needs to be sorted according to String length
- Ascending-order sorting :
- Create TreeSet passing Comparator as constructor-argument with ascending-order of String length logic
- Add original String List elements to newly created TreeSet using addAll() method
- Descending-order sorting :
- Create TreeSet passing Comparator as constructor-argument with descending-order of String length logic
- Add original String List elements to newly created TreeSet using addAll() method
- Print TreeSet object to the console for both ascending-order and descending-order of String List in accordance with its String length
- Note: If there are multiple Strings of same length in the String List then this will discard elements of same length as Set doesn’t allows duplicates
- Check below example 2 if there are multiple Strings of same length
SortingStringListByItsLengthUsingTreeSet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package in.bench.resources.sorting.string.list; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.TreeSet; public class SortingStringListByItsLengthUsingTreeSet { public static void main(String[] args) { // 1. names with different length List<String> names = Arrays.asList( "Bond" , "Einstein" , "Alice" , "Whitman" , "Lee" , "Spider" ); // 1.1 print to console System.out.println( "Original String List :- \n" + names + "\n" ); // 2. sorting String List in Ascending-order Set<String> treeSetAsc = new TreeSet<>( new Comparator<String>() { @Override public int compare(String str1, String str2) { return str1.length() - str2.length(); } }); // 2.1 add String List items to TreeSet treeSetAsc.addAll(names); // 2.2 print ascending-order sorted Strings by its Length System.out.println( "\nAscending-order Sorted String List by its Length :- \n" + treeSetAsc + "\n" ); // 3. sorting String List in Descending-order Set<String> treeSetDesc = new TreeSet<>( new Comparator<String>() { @Override public int compare(String str1, String str2) { return str2.length() - str1.length(); } }); // 3.1 add String List items to TreeSet treeSetDesc.addAll(names); // 3.2 print descending-order sorted Strings by its Length System.out.print( "\nDescending-order Sorted String List by its Length :- \n" + treeSetDesc); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | Original String List :- [Bond, Einstein, Alice, Whitman, Lee, Spider] Ascending-order Sorted String List by its Length :- [Lee, Bond, Alice, Spider, Whitman, Einstein] Descending-order Sorted String List by its Length :- [Einstein, Whitman, Spider, Alice, Bond, Lee] |
2. 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,
- Override/implement compare() method of Comparator interface
- Inside method implementation return difference of String length
- For Ascending-order sorting,
- subtract 2nd String length from the 1st String length
- Syntax:- str1.length() – str2.length();
- For Descending-order sorting,
- subtract 1st String length from the 2nd String length
- Syntax:- str2.length() – str1.length();
- Print both ascending-order and descending-order sorted String List in accordance with its String length to the console
SortingStringListByItsLengthUsingCollectionsSortMethod.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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 SortingStringListByItsLengthUsingCollectionsSortMethod { 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, new Comparator<String>() { @Override public int compare(String str1, String str2) { return 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 Collections.sort(names, new Comparator<String>() { @Override public int compare(String str1, String str2) { return 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:
1 2 3 4 5 6 7 8 9 10 | 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 – Sorting ArrayList using Comparable and Comparator
- Java – Sorting ArrayList in descending order
- Java – How to sort LinkedList using Collections.sort() method ?
- Java – How to sort Vector using Collections.sort() method ?
- Java – Sorting list of objects on multiple fields using Comparator
- Java – Sorting HashSet contents in ascending and descending order
- Java – How to Sort HashSet in 2 ways ?
- Java 8 – How to sort HashSet ?
- Java – How to sort LinkedHashSet contents ?
- Java – How to sort TreeSet in descending order using Comparator ?
- Java – Sorting Collection of String, StringBuffer and StringBuilder
- Java – How to Sort String List by its length in Ascending and Descending order ?
References:
- https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/List.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
Happy Coding !!
Happy Learning !!