isSorted

Returns true if each element in the sequence is less than or equal to the following element according to their natural sort order.

Returns true if the sequence has fewer than two elements.

The elements are compared sequentially using Comparable.compareTo, and the sequence is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Note that the result depends on the iteration order of the sequence. The iteration order of some Sequence implementations may be unstable (change from one invocation to the next), in which case this function may return inconsistent results.

For elements of floating-point types (Double, Float), NaN is considered greater than any other value (including positive infinity), and -0.0 is considered less than 0.0, consistent with Double.compareTo and Float.compareTo.

The operation is terminal.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(sequenceOf<String>().isSorted()) // true
println(sequenceOf("apple").isSorted()) // true

val sorted = sequenceOf("apple", "banana", "cherry")
println(sorted.isSorted()) // true

val unsorted = sequenceOf("banana", "apple", "cherry")
println(unsorted.isSorted()) // false 
   //sampleEnd
}