Arrange a list in alphabetical order.
import public |
The sort() method organizes items within the list. A Comparator, defined by a lambda expression compatible with Java’s Comparator interface’s compare() method, can be utilized to compare pairs of elements.
If null is provided to the method, items will be naturally sorted based on their data type (e.g., alphabetically for strings, numerically for numbers). Non-primitive types must implement Java’s Comparable interface for sorting without a comparator.
public |
Parameter |
Description |
compare |
Mandatory: A comparator or lambda expression that compares pairs of items in the list. Use null to compare items naturally based on their data type. |
Java version: |
1.8+ |
import |