In this article, we will discuss how to sort String[] Arrays by its length in Ascending-order and Descending-order
Sorting String[] Arrays by its length:
- Using TreeSet (inter-conversion constructor)
- Using Arrays.sort() method
1. Using TreeSet :
- There is a String[] Arrays 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[] Arrays elements to newly created TreeSet using addAll() method after converting String[] Arrays into List
- Descending-order sorting :
- Create TreeSet passing Comparator as constructor-argument with descending-order of String length logic
- Add original String[] Arrays elements to newly created TreeSet using addAll() method after converting String[] Arrays into List
- Print TreeSet object to the console for both ascending-order and descending-order of String[] Arrays in accordance with its String length
- Note: If there are multiple Strings of same length in the String[] Arrays 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
SortingStringArraysByItsLengthUsingTreeSet.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.arrays; import java.util.Arrays; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; public class SortingStringArraysByItsLengthUsingTreeSet { public static void main(String[] args) { // 1. names with different length String[] names = new String[] { "Bond" , "Einstein" , "Alice" , "Whitman" , "Bob" , "Spider" }; // 1.1 print to console System.out.println( "Original String[] Arrays :- \n" + Arrays.toString(names) + "\n" ); // 2. sorting String[] Arrays 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[] Arrays items to TreeSet treeSetAsc.addAll(Arrays.asList(names)); // 2.2 print ascending-order sorted Strings by its Length System.out.println( "\nAscending-order Sorted String[] Arrays by its Length :- \n" + treeSetAsc + "\n" ); // 3. sorting String[] Arrays 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[] Arrays items to TreeSet treeSetDesc.addAll(Arrays.asList(names)); // 3.2 print descending-order sorted Strings by its Length System.out.print( "\nDescending-order Sorted String[] Arrays by its Length :- \n" + treeSetDesc); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | Original String[] Arrays :- [Bond, Einstein, Alice, Whitman, Bob, Spider] Ascending-order Sorted String[] Arrays by its Length :- [Bob, Bond, Alice, Spider, Whitman, Einstein] Descending-order Sorted String[] Arrays by its Length :- [Einstein, Whitman, Spider, Alice, Bond, Bob] |
2. 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,
- 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[] Arrays in accordance with its String length to the console
SortingStringArraysByItsLengthUsingArraysSortMethod.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 | package in.bench.resources.sorting.string.arrays; import java.util.Arrays; import java.util.Comparator; public class SortingStringArraysByItsLengthUsingArraysSortMethod { 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, 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[] Arrays " + "by its Length :- \n" + Arrays.toString(names) + "\n" ); // 3. sorting String[] Arrays in Descending-order Arrays.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[] Arrays " + "by its Length :- \n" + Arrays.toString(names)); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | 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:
- Byte Arrays sorting
- char Arrays sorting
- short Arrays sorting
- Integer Arrays sorting
- Float Arrays sorting
- Double Arrays sorting
- Long Arrays sorting
- String Arrays sorting
- Java – How to Sort Arrays in Ascending and Descending order ?
- Java – String Arrays sorting in ascending & descending order
- Java – Sorting after merging two String[] Arrays
- Java – Sorting Arrays using Comparable and Comparator interface
- Java – How to Sort String[] arrays by its length in Ascending and Descending order ?
- 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/Arrays.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
Happy Coding !!
Happy Learning !!