A List is an interface that is part of the Java Collection framework. It can be used to store a List of objects. In this article, I will be explaining the different ways in which you can sort a List.

  1. Via List.sort
  2. Via Collections.sort()
  3. Via Streams
  4. Sorting a List of custom objects

How to Sort a List in Java

Via List.sort()

The List interface has a method called sort. It accepts as a parameter a Comparator instance and sorts the List on which it is invoked as per the specified Comparator.

Sample Code

List<Integer> myList = Arrays.asList(76,23,91,45,21,82);
System.out.println("Unsorted List:"+myList);
myList.sort(Comparator.naturalOrder());
System.out.println("Sorted List:"+myList);
  • Line 1 creates an Integer List called myList and initializes it with some values.
  • Line 3 invokes the sort method on myList. It obtains a Comparator via the Comparator.naturalOrder method and passes this to the sort method. The Comparator.naturalOrder method returns a Comparator instance that sorts as per the natural order. In this case, the input List contains Integers for which the natural order is ascending. So, Line 3 sorts myList in the ascending order of values.

Output

Unsorted List:[76, 23, 91, 45, 21, 82]
Sorted List:[21, 23, 45, 76, 82, 91]

Via Collections.sort()

The Collections class also has a method called sort which can be used to sort a List. It accepts as parameter a List and sorts the List as per its natural order.

Sample Code

List<String> myList = Arrays.asList("Red","Black","White","Pink");
System.out.println("Unsorted List:"+myList);
Collections.sort(myList);
System.out.println("Sorted List:"+myList);
  • Line 1 creates a String List called myList and initializes it with some values.
  • Line 3 invokes the Collections.sort method passing myList as a parameter.
  • The Collections.sort sorts the input List in the natural order. In this case, since myList contains Strings, the natural order is alphabetical. So, Line 3 sorts myList in alphabetical order.

Output

Unsorted List:[Red, Black, White, Pink]
Sorted List:[Black, Pink, Red, White]

There is an overloaded version of the Collections.sort method. In addition to the List to be sorted, it also accepts as parameter a Comparator and sorts the input List as per the specified Comparator.

Sample Code

List<String> myList = Arrays.asList("Red","Black","White","Pink");
System.out.println("Unsorted List:"+myList);
Collections.sort(myList,Comparator.reverseOrder());
System.out.println("Sorted List:"+myList);
  • As before, Line 1 creates a String List called myList.
  • Line 3 invokes the Collections.sort method passing myList as a parameter. In addition, it obtains a Comparator via the Comparator.reverseOrder method and passes this to the sort method. The Comparator. reverseOrder method returns a Comparator instance that sorts in the reverse of the natural order. In this case, since the input List contains Strings, the natural order is alphabetical order. So, the sort method sorts myList in the reverse alphabetical order.

Output

Unsorted List:[Red, Black, White, Pink]
Sorted List:[White, Red, Pink, Black]

Via Streams

The Stream interface has a method called sorted. This sorts the stream on which it is invoked as per its natural order. Thus, a Stream can be obtained from a List and the sorted method can be used to sort it.

Sample Code

List<Double> myList = Arrays.asList(54.1,32.7, 65.4,18.2,98.5);
System.out.println("Unsorted List:"+myList);
List<Double> sortedList = myList.stream().sorted().collect(Collectors.toList());
System.out.println("Sorted List:"+sortedList);
  • Line 1 creates a Double List called myList and initializes it with some values.
  • Line 3 first invokes the stream method on myList. This returns a Stream corresponding to myList. It then invokes the sorted method which sorts the Stream as per its natural order. In this case, the stream contains Double values for which the natural order is ascending, so the sorted method sorts the stream in ascending order. The collect method is then invoked which converts the Stream back to a List. This is assigned to the sortedList variable.

Output

Unsorted List:[54.1, 32.7, 65.4, 18.2, 98.5]
Sorted List:[18.2, 32.7, 54.1, 65.4, 98.5]

There is an overloaded version of the sorted method. It accepts as parameter a Comparator and sorts the stream as per the specified Comparator.

Sample Code

List<Double> myList = Arrays.asList(54.1,32.7, 65.4,18.2,98.5);
System.out.println("Unsorted List:"+myList);
List<Double> sortedList = myList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println("Sorted List:"+sortedList);

  • As before, Line 1 creates a Double List called myList.
  • Line 3 obtains a stream on myList and invokes the sorted method on it. It obtains a Comparator via the Comparator.reverseOrder method and passes it to the sorted method. In this case, since the input List contains Double values, the natural order is ascending order, so Comparator.reverseOrder returns a Comparator that sorts in descending order. So, the sorted method sorts the stream in descending order which is then converted to a List via the collect method.

Output

Unsorted List:[54.1, 32.7, 65.4, 18.2, 98.5]
Sorted List:[98.5, 65.4, 54.1, 32.7, 18.2]

Sorting a List of custom objects

In the code so far, we had seen how to sort a List containing primitive types or Strings. Let us now see how to sort a List containing some custom objects.

Sample Code

public class Product {

 private String name;
 private int price;
 private String category;
 
 // constructor and getter setters
}

This code specifies a class called Product. In order to sort a List of Product objects, you can use any of the approaches listed above. However, you will need to use the versions of the methods that accept a Comparator and pass in a Comparator that specifies how to sort the List.

Sample Code

List<Product> myList = new ArrayList<>();
myList.add(new Product("Television",300,"Electronics"));
myList.add(new Product("Computer",249,"Electronics"));
myList.add(new Product("Bed",657,"Furniture"));
Collections.sort(myList,(p1,p2) -> p1.getPrice() - p2.getPrice());
System.out.println("Sorted List:");
myList.forEach(p1 -> System.out.println(p1.getName()));
  • Line 1 creates a Product List called myList.
  • Lines 2-4 add some Product objects to this List
  • Line 5 invokes the Collections.sort method. As mentioned earlier, the Collections.sort method accepts as parameter the List to be sorted and a Comparator instance. In this case, the Comparator is implemented via a lambda expression that returns the difference of the price fields. So, this results in the Product objects being sorted in the ascending order of price.
  • Instead of the Collections.sort, the List.sort method or the stream.sorted method can also be used, however, a custom Comparator will need to be passed to these methods as done above.

Output

Sorted List:
Computer
Television
Bed

The Comparator interface also has a method called comparing. It accepts as parameter a Function instance that can be used to retrieve a sort key. It returns a Comparator that sorts on the specified key.

Sample Code

List<Product> myList = new ArrayList<>();
myList.add(new Product("Television",300,"Electronics"));
myList.add(new Product("Computer",249,"Electronics"));
myList.add(new Product("Bed",657,"Furniture"));
Comparator<Product> c1 = Comparator.comparing(p1 -> p1.getName());
Collections.sort(myList,c1);
System.out.println("Sorted List:");
myList.forEach(p1 -> System.out.println(p1.getName()));
  • As before, Line 1 creates a Product List called myList, and Lines 2-4 add some Product objects to this List
  • Line 5 invokes the Comparator.comparing method. This accepts as parameter a Function instance. In this case, this is implemented via a lambda expression that accepts a Product object and returns the name field. So, the Comparator.comparing returns a Comparator that sorts the Product objects based on the name field.
  • Line 6 then invokes the Collections.sort method by passing in the Comparator c1. So, this results in the Product objects being sorted based on the name field.

Output

Sorted List:
Bed
Computer
Television

Conclusion

So, in this article, we saw the various ways in which you can sort a Java List. First, we saw how to sort a List via the List.sort, Collections.sort, and the stream.sorted method. We also saw how to pass a Comparator object to these methods in order to specify the sorting order. Finally, we saw the different ways in which a List of custom objects can be sorted.