In this post, we’re going to explore different ways to sort lists alphabetically in Java. We’ll cover various sorting approaches using Java Streams, Java Collections, TreeSet, Comparator, and the RuleBasedCollator functionality.
Sort Lists Alphabetically with Java Streams
With the introduction of Java 8, Streams were introduced in Java. Let’s see how we can sort lists alphabetically in Java using streams.
To perform sorting with Streams, we use the sorted
method, which returns a Stream of Strings.
Natural Alphabetical Sorting
In the following example, we use sorted()
to obtain natural alphabetical sorting. Let’s see it in the following test:
@Test void given_name_list_when_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); List<String> sortedNames = names.stream() .sorted() .collect(Collectors.toList()); List<String> expectedNames = List.of("Aman","John","Noel","Pablo","Pepe","Susan"); assertThat(sortedNames).isEqualTo(expectedNames); }
Reverse Alphabetical Sorting
To perform reverse sorting, we can use the reverseOrder
method of Comparator or a lambda function.
@Test void given_name_list_when_reverse_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); List<String> sortedNames = names.stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); List<String> expectedNames = List.of("Susan","Pepe", "Pablo", "Noel", "John", "Aman"); assertThat(sortedNames).isEqualTo(expectedNames); }
Alphabetical Sorting with Java Collection
Java Collection provides the sort
method for performing natural sorting and the Comparator
for performing reverse sorting.
Natural Sorting with Collection in Java
The sort
method can be used on any Collection and it returns the fully sorted list. If no comparator is applied, the list is sorted in natural order (A to Z).
Let’s see how to use sort()
on Java collections:
@Test void given_name_list_when_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); Collections.sort(names); List<String> expectedNames = List.of("Aman","John","Noel","Pablo","Pepe","Susan"); assertThat(sortedNames).isEqualTo(expectedNames); }
Reverse Sorting with Collection in Java
Let’s perform reverse sorting using Comparator
. Since Comparator
is a functional interface, it can be used in a lambda expression.
@Test void given_name_list_when_reverse_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); Collections.sort(names, Comparator.reverseOrder()); List<String> expectedNames = List.of("Susan","Pepe", "Pablo", "Noel", "John", "Aman"); assertThat(sortedNames).isEqualTo(expectedNames); }
Natural Sorting in Lists in Java
Java lists have their own sort
method for sorting instead of using Collection.sort()
.
@Test void given_name_list_when_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); names.sort(); List<String> expectedNames = List.of("Aman","John","Noel","Pablo","Pepe","Susan"); assertThat(sortedNames).isEqualTo(expectedNames); }
Reverse Sorting with List in Java
To perform reverse sorting of a list, we can apply Comparator.reverseOrder()
.
@Test void given_name_list_when_inverse_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); names.sort(Comparator.reverseOrder()); List<String> expectedNames = List.of("Susan","Pepe", "Pablo", "Noel", "John", "Aman"); assertThat(sortedNames).isEqualTo(expectedNames); }
Performing Sorting in Java with Rule-Based Collator
When using the RuleBasedCollator
class, we can define a series of steps or rules to define an order in a list.
Let’s see an example where we use a rule to create a new sorting:
@Test void given_name_list_when_request_to_sort_with_rule_based_collector_then_list_is_returned() throws ParseException { List<String> names = Arrays.asList( "Pepe","Pablo","Noel","Susan","John","Aman"); Collections.sort(names); List<String> namesOrderExpected = Arrays.asList( "Aman","John","Noel","Pablo","Pepe","Susan"); assertThat(names).isEqualTo(namesOrderExpected); List<String> rulesBasedExpected = Arrays.asList( "Noel","Pablo","Pepe","Susan","Aman","John"); String rule = "< n, N < P, S < a, A < j, J"; RuleBasedCollator rulesCollator = new RuleBasedCollator(rule); names.sort(rulesCollator); assertThat(names).isEqualTo(rulesBasedExpected); }
In the above example, we have altered the logical order using the RuleBasedCollector
object, where we have established the order: “< n, N < P, S < a, A < j, J”.
Sorting in Java with TreeSet
It’s not very common to use TreeSet
, but using this class allows us to perform alphabetical sorting in Java.
TreeSet
enables natural and reverse alphabetical sorting. The only condition is that it does not retain duplicate elements.
@Test void given_name_list_when_sort_then_return_a_list_sorted() { List<String> names = List.of("Pepe","Pablo","Noel","Susan","John","Aman"); SortedSet<String> sortedTreeSet = new TreeSet<>(names); List<String> sortedList = new ArrayList<>(sortedTreeSet); List<String> expectedNames = List.of("Aman","John","Noel","Pablo","Pepe","Susan"); assertThat(sortedNames).isEqualTo(expectedNames); }
Conclusion
In this post about sort lists alphabetically in Java, we have seen different approaches to perform list sorting.
If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!