The Java 8 way of doing this is to use List.sort as follows:
personList.sort(Comparator.comparing(Person::getName));
To quote Stuart Marks in his answer over here.
This is the big advantage of the List.sort(cmp)
extension method over Collections.sort(list, cmp)
. It might seem that this is merely a small syntactic advantage being able to write myList.sort(cmp)
instead of Collections.sort(myList, cmp)
. The difference is that myList.sort(cmp)
, being an interface extension method, can be overridden by the specific List
implementation. For example, ArrayList.sort(cmp)
sorts the list in-place using Arrays.sort()
whereas the default implementation implements the old copyout-sort-copyback technique.