You can sort ArrayList and LinkedList of strings, numbers, dates and objects by using Collections.sort
or using ArrayList and LinkedList instances sort
method since Java 8+
Lets walk through this tutorial to see the detail examples
Sort ArrayList and LinkedList of strings, numbers and dates
You can use Comparator.naturalOrder()
to sort in ascending order and Comparator.reverseOrder()
to sort in descending order along with the sort method of ArrayList and LinkedList instances
The following give you examples on ArrayLists but you can also apply the same on LinkedLists
- Sort an ArrayList of strings in alphabetical and reversed order
@Test
public void sortStrings(){
List<String> strings = new ArrayList<>(List.of("c", "a", "b"));
strings.sort(Comparator.naturalOrder());
assertThat(strings).containsExactly("a", "b", "c");
strings.sort(Comparator.reverseOrder());
assertThat(strings).containsExactly("c", "b", "a");
}
- Sort an ArrayList of numbers in ascending and descending order
public void sortNumbers() {
List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2));
numbers.sort(Comparator.naturalOrder());
assertThat(numbers).containsExactly(1, 2, 3);
numbers.sort(Comparator.reverseOrder());
assertThat(numbers).containsExactly(3, 2, 1);
}
- Sort an ArrayList of dates in chronological and reversed order
@Test
public void sortDates() {
LocalDate ld1 = LocalDate.of(2019, 3, 1);
LocalDate ld2 = LocalDate.of(2019, 1, 1);
LocalDate ld3 = LocalDate.of(2019, 2, 1);
List<LocalDate> dates = new ArrayList<>(List.of(ld1, ld2, ld3));
dates.sort(Comparator.naturalOrder());
assertThat(dates).containsExactly(ld2, ld3, ld1);
dates.sort(Comparator.reverseOrder());
assertThat(dates).containsExactly(ld1, ld3, ld2);
}
Sort an ArrayList and LinkedList of objects by one or multiple fields
Sort ArrayList and LinkedList of use defined objects by one field with
Comparator.comparing(keyExtractor, keyComparator)
keyExtractor
is a function used to extract the sort keykeyComparator
is an optionalComparator
, the default isComparator.naturalOrder
, used to compare the sort key
@Test
public void sortOneField() {
Book book1 = new Book(1, "b");
Book book2 = new Book(2, "c");
Book book3 = new Book(3, "a");
List<Book> list = Arrays.asList(book1, book2, book3);
list.sort(Comparator.comparing(Book::getTitle));
assertThat(list).containsExactly(book3, book1, book2);
}
Book is defined as the following
class Book {
int id;
String title;
Book(int id, String title) {
this.id = id;
this.title = title;
}
int getId() {
return id;
}
String getTitle() {
return title;
}
}
- Sort ArrayList and LinkedList of objects by multiple fields with
Comparator.comparing(keyExtractor, keyComparator).thenComparing(keyExtractor, keyComparator)
@Test
public void sortMultipleFields() {
Book book1 = new Book(1, "b");
Book book2 = new Book(2, "c");
Book book3 = new Book(3, "c");
List<Book> list = Arrays.asList(book1, book2, book3);
list.sort(Comparator
.comparing(Book::getTitle, Comparator.reverseOrder())
.thenComparing(Book::getId, Comparator.reverseOrder()));
assertThat(list).containsExactly(book3, book2, book1);
}
Comparable and Comparator
In the previous example, you can implement the compareTo
method contract of Comparable
interface to provide the default sort key extractor and default ordering to the underlying objects, if you don't like to specify the Comparator.comparing
chain explicitly when executing the sort method
Learn more about Comparable and Comparator
Don't sort a List with null items
ArrayList and LinkedList accept null
elements, however, NullPointerException
will be thrown if you try to sort one with null elements
@Test
public void sortNullObjects() {
List<Integer> lst = new ArrayList<>(Arrays.asList(3, 1, null));
ThrowableAssert.ThrowingCallable c = () -> lst.sort(Comparator.naturalOrder());
assertThatThrownBy(c).isInstanceOf(NullPointerException.class);
}
Collections.sort
You can also use Collections.sort(List, Comparator)
methods to sort a List in Java. They internally call to the specified list's sort method which in turn also dispatches to the Java 8+ sort(Comparator)
default method of the List
interface
Conclusion
In this tutorial, we learned using sort(Comparator) method of ArrayList and LinkedList instances to sort strings, numbers, dates and user defined objects. You can find below the full source code