isSortedWith

fun <T> Sequence<T>.isSortedWith(comparator: Comparator<in T>): Boolean(source)

Returns true if each element in the sequence is less than or equal to the following element according to the specified comparator.

Returns true if the sequence has fewer than two elements.

The elements are compared sequentially using Comparator.compare, 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.

The operation is terminal.

Since Kotlin

2.4

Samples


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

println(sequenceOf("apple", "banana", "cherry").isSortedWith(naturalOrder())) // true
println(sequenceOf("apple", "banana", "cherry").isSortedWith(reverseOrder())) // false

println(sequenceOf("cherry", "banana", "apple").isSortedWith(reverseOrder())) // true

println(sequenceOf("Apple", "banana", "Cherry").isSortedWith(String.CASE_INSENSITIVE_ORDER)) // true

println(sequenceOf(null, "apple", "banana").isSortedWith(nullsFirst(naturalOrder()))) // true
println(sequenceOf(null, "apple", "banana").isSortedWith(nullsLast(naturalOrder()))) // false 
   //sampleEnd
}