I know from official documentation that compareBy
creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects
.
I know how this must be done for normal attributes like the integer value here, but how are boolean conditions handled by compareBy?
In this example, I intended to keep all 4's at the top of the list and then sort in ascending order of values, but I am not sure how this boolean expression helps me do this!
fun main(args: Array<String>) {
var foo = listOf(2, 3, 4, 1, 1, 5, 23523, 4, 234, 2, 2334, 2)
foo = foo.sortedWith(compareBy({
it != 4
},{
it
}))
print(foo)
}
Output
[4, 4, 1, 1, 2, 2, 2, 3, 5, 234, 2334, 23523]