Ordering
The order of elements is an important aspect of certain collection types. For example, two lists of the same elements are not equal if their elements are ordered differently.
In Kotlin, the orders of objects can be defined in several ways.
First, there is natural order. It is defined for implementations of the Comparable
interface. Natural order is used for sorting them when no other order is specified.
Most built-in types are comparable:
Numeric types use the traditional numerical order:
1
is greater than0
;-3.4f
is greater than-5f
, and so on.Char
andString
use the lexicographical order:b
is greater thana
;world
is greater thanhello
.
To define a natural order for a user-defined type, make the type an implementer of Comparable
. This requires implementing the compareTo()
function. compareTo()
must take another object of the same type as an argument and return an integer value showing which object is greater:
Positive values show that the receiver object is greater.
Negative values show that it's less than the argument.
Zero shows that the objects are equal.
Below is a class for ordering versions that consist of the major and the minor part.
Custom orders let you sort instances of any type in a way you like. Particularly, you can define an order for non-comparable objects or define an order other than natural for a comparable type. To define a custom order for a type, create a Comparator
for it. Comparator
contains the compare()
function: it takes two instances of a class and returns the integer result of the comparison between them. The result is interpreted in the same way as the result of a compareTo()
as is described above.
Having the lengthComparator
, you are able to arrange strings by their length instead of the default lexicographical order.
A shorter way to define a Comparator
is the compareBy()
function from the standard library. compareBy()
takes a lambda function that produces a Comparable
value from an instance and defines the custom order as the natural order of the produced values.
With compareBy()
, the length comparator from the example above looks like this:
The Kotlin collections package provides functions for sorting collections in natural, custom, and even random orders. On this page, we'll describe sorting functions that apply to read-only collections. These functions return their result as a new collection containing the elements of the original collection in the requested order. To learn about functions for sorting mutable collections in place, see the List-specific operations.
Natural order
The basic functions sorted()
and sortedDescending()
return elements of a collection sorted into ascending and descending sequence according to their natural order. These functions apply to collections of Comparable
elements.
Custom orders
For sorting in custom orders or sorting non-comparable objects, there are the functions sortedBy()
and sortedByDescending()
. They take a selector function that maps collection elements to Comparable
values and sort the collection in natural order of that values.
To define a custom order for the collection sorting, you can provide your own Comparator
. To do this, call the sortedWith()
function passing in your Comparator
. With this function, sorting strings by their length looks like this:
Reverse order
You can retrieve the collection in the reversed order using the reversed()
function.
reversed()
returns a new collection with the copies of the elements. So, if you change the original collection later, this won't affect the previously obtained results of reversed()
.
Another reversing function - asReversed()
returns a reversed view of the same collection instance, so it may be more lightweight and preferable than
reversed()
if the original list is not going to change.
If the original list is mutable, all its changes reflect in its reversed views and vice versa.
However, if the mutability of the list is unknown or the source is not a list at all, reversed()
is more preferable since its result is a copy that won't change in the future.
Random order
Finally, there is a function that returns a new List
containing the collection elements in a random order - shuffled()
. You can call it without arguments or with a Random
object.