Sort list of strings alphabetically

This article will cover various ways to sort a list of strings alphabetically in java, both in ascending and descending(reverse) orders with examples.

1. Collections.sort()

java.util.Collections class has a static sort() method, which accepts a collection argument and sorts its elements in natural ordering.
Natural ordering implies strings in alphabetical order, numbers in increasing value etc. Example,

List<String> list = new ArrayList<>(); 
list.add("google");
list.add("youtube");
list.add("facebook");
list.add("amazon");
System.out.println("Before sorting: " + list);
Collections.sort(list);
System.out.println("After sorting: " + list);

Output is

Before sorting: [google, youtube, facebook, amazon]
After sorting: [amazon, facebook, google, youtube]

Note that for sort() to work, the type of list must implement Comparable interface. If it does not, then you will get a compiler error

The method sort(List<T>) in the type Collections is not applicable for the arguments (List<User>)

Also, remember that sort() modifies the original collection. So, you do not need to assign it to a new variable.

2. Streams

Java 8 streams can be used to sort a list.
A new method stream() is added to List interface in java 8. stream() is a default interface method, which returns a stream over list elements.
Once you get a stream, call sorted() method on stream.
sorted() will return another stream with elements sorted in ascending order.
This new stream can be converted into a list with collect() method as shown below.

List<String> list = new ArrayList<>();
list.add("google");
list.add("youtube");
list.add("facebook");
list.add("amazon");
System.out.println("Before sorting: " + list);
list = list.stream().sorted().collect(Collectors.toList());
System.out.println("After sorting: " + list);

This is a method to sort a list of strings without using collections in java.

3. Comparator

Though Collections.sort() sorts the list alphabetically be default. Still, if you need to sort it using Comparator interface, then go through this method.

There is an overloaded version of sort(), which accepts an object of type Comparator as second argument and sorts the list according to the logic defined in it.
So, a list can be sorted alphabetically using Comparator as shown below

List<String> list = new ArrayList<>();
list.add("google");
list.add("youtube");
list.add("facebook");
list.add("amazon");
System.out.println("Before sorting: " + list);
Collections.sort(list, new Comparator<String>() {
   @Override
   public int compare(String s1, String s2) {
     return s1.compareTo(s2);
   }
});
System.out.println("After sorting: " + list);

Since Comparator is a Functional interface, we can directly supply its implementation as a Lambda expression as shown below

List<String> list = new ArrayList<>(); 
list.add("google"); 
list.add("youtube"); 
list.add("facebook"); 
list.add("amazon"); 
System.out.println("Before sorting: " + list); 
Collections.sort(list, (s1, s2) -> s1.compareTo(s2)); 
System.out.println("After sorting: " + list);

Lambda expressions will only work with java 8 and above.

Another method to create a Comparator to sort list alphabetically is using its static naturalOrder() method as shown below

List<String> list = new ArrayList<>(); 
// add elements to list 

Collections.sort(list, Comparator.naturalOrder());

naturalOrder() was added in java 8.

4. List.sort()

A new method sort() was introduced in List interface in java 8.
This method accepts a Comparator argument and sorts the list according to its sorting logic as shown below

List<String> list = new ArrayList<>();
// add elements to list
list.sort((s1, s2) -> s1.compareTo(s2));

Sorting a list in reverse order

Reverse order sorting means arranging list elements in reverse alphabetic order.

1. Collections.sort()

As stated above, there is an overloaded sort() method which accepts a Comparator as second argument.
If the comparator is implemented to return the bigger element, then sort() method will arrange list elements in reverse order. Example,

List<String> list = new ArrayList<>();
list.add("google");
list.add("youtube");
list.add("facebook");
list.add("amazon");
System.out.println("Before sorting: " + list);
Collections.sort(list, (s1, s2) -> s2.compareTo(s1));
System.out.println("After sorting: " + list);

Output is

Before sorting: [google, youtube, facebook, amazon]
After sorting: [youtube, google, facebook, amazon]

2. Comparator.reverseOrder()

Java 8 added reverseOrder() method to Comparator interface, which compares the elements in reverse order.
Above method can be modified to use reverseOrder() as below

List<String> list = new ArrayList<>(); 
// add elements to list

Collections.sort(list, Comparator.reverseOrder()); 

Hope the article was useful.