This example will show how to sort an arraylist of objects using java, java 8 and guava. In the setup we will create a Wrestler class and initialize a list of wrestlers of varying weight, names and wins.
Setup
Wrestler class
Initialize Wrestlers
Straight up Java
In this snippet we will sort a collection by a field using java. We will implement a Comparator naming it byWeightClass that will compare each wrestlers by weight class and pass it to Collections.sort which will sort the list according to the order defined in the comparator. After this operation is executed the list will be 'sorted by weight class' in ascending order.
Output
Java 8
Order by number
In this snippet we will order the collection based off an integer field in the Wrestler object with java 8. Using a lambda expression we will create a comparator that will compare the weight of two given Wrestler objects. We will pass it into the List.sort method that will sort the list based on the supplied Comparator. Once the operation is performed the list of wrestlers will be sorted by weight class in ascending order.
Output
Reverse order
This snippet will sort a collection in descending order with java 8. We will create a Comparator field from a lambda expression and call reverse which will imposes the reverse ordering of this comparator. Then calling the List.sort method we will sort the list of wrestlers by weight class in reverse order.
Output
Order by string
This snippet will show how to sort a collection of object by string field with java 8. Using a lambda expression we will create a comparator that will compare the name of two given Wrestler objects. We will pass it into the List.sort method that will sort the list based on the supplied Comparator. Once the operation is performed the list of wrestlers will be sorted by name in ascending order.
Output
Chaining multiple comparators
This snippet will demonstrate how to sort the collection on multiple fields or chain multiple comparators using java 8. We will first sort the collection of wrestlers by weight class then by the number of wins by passing in two comparators created by a lambda expression.
Output
Google Guava
Guava's ordering class is an enhanced comparator, with additional methods to support common operations. If are familiar with the FluentIterable, FluentIterable is to Iterable as Ordering is to comparator.
Ordering
This snippet will show how to sort a list of strings in alphabetical order with Guava. First byName Ordering object is created and passed to the Collections.sort which will sort the array list in ascending order.
Output
Ordering in reverse
This snippet will sort the collection from highest to lowest with Guava. Creating a Ordering object byWeightClass and passing it into Collections.Sort will sort the wrestlers in descending order.
Output
Chaining multiple ordering
This snippet will chain multiple Ordering objects by calling the compound method. First it will be sorted by weightClass then by wins.
Output
Multiple ordering & .getFirst
Finally this snippet will display top wrestlers in each weight class.