16.7.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. For example, the natural order for numeric values is ascending order, and the natural order for Strings is based on their lexicographical ordering (Section 14.3). 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 Order
Figure 16.6 uses Collections
method sort
to order the elements of a List
in ascending order (line 17). Method sort ...
Get Java™ How To Program (Early Objects), Tenth 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.