J.6.1. Method sort
Method sort sorts the elements of a List
, which must implement the Comparable interface. The order is determined by the natural order of the elements’ type as implemented by a compareTo
method. Method compareTo
is declared in interface Comparable
and is sometimes called the natural comparison method. The sort
call may specify as a second argument a Comparator object that determines an alternative ordering of the elements.
Sorting in Ascending or Descending Order
If list
is a List
of Comparable
objects (such as String
s), you can use Collections
method sort
to order the elements in ascending order as follows:
Collections.sort( list ); // sort list into ascending order
You can sort the List
in ...
Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.