Kotlin

Collection Ordering

Swift
                  class Version(val major: Int, val minor: Int): Comparable<Version> {
    override fun compareTo(other: Version): Int {
        if (this.major != other.major) {
            return this.major - other.major
        } else if (this.minor != other.minor) {
            return this.minor - other.minor
        } else return 0
    }
}
​
fun main() {    
    println(Version(1, 2) > Version(1, 3))
    println(Version(2, 0) > Version(1, 5))
}
                
                    class Version(let major: Int, let minor: Int): Comparable<Version> {
    override func compareTo(other: Version) -> Int {
        if (this.major != other.major) {
            return this.major - other.major
        } else if (this.minor != other.minor) {
            return this.minor - other.minor
        } else return 0
    }
}
​
func main() {    
    print(Version(1, 2) > Version(1, 3))
    print(Version(2, 0) > Version(1, 5))
}
                  
                  val lengthComparator = Comparator { str1: String, str2: String -> str1.length - str2.length }
println(listOf("aaa", "bb", "c").sortedWith(lengthComparator))
                
                    let lengthComparator = Comparator { str1: String, str2: String -> str1.length - str2.length }
print(listOf("aaa", "bb", "c").sortedWith(lengthComparator))
                  
                  ​
println(listOf("aaa", "bb", "c").sortedWith(compareBy { it.length }))
                
                    ​
print(listOf("aaa", "bb", "c").sortedWith(compareBy { it.length }))
                  

Natural order

                  val numbers = listOf("one", "two", "three", "four")
​
println("Sorted ascending: ${numbers.sorted()}")
println("Sorted descending: ${numbers.sortedDescending()}")
                
                    let numbers = listOf("one", "two", "three", "four")
​
print("Sorted ascending: ${numbers.sorted()}")
print("Sorted descending: ${numbers.sortedDescending()}")
                  

Custom orders

                  val numbers = listOf("one", "two", "three", "four")
​
val sortedNumbers = numbers.sortedBy { it.length }
println("Sorted by length ascending: $sortedNumbers")
val sortedByLast = numbers.sortedByDescending { it.last() }
println("Sorted by the last letter descending: $sortedByLast")
                
                    let numbers = listOf("one", "two", "three", "four")
​
let sortedNumbers = numbers.sortedBy { it.length }
print("Sorted by length ascending: $sortedNumbers")
let sortedByLast = numbers.sortedByDescending { it.last() }
print("Sorted by the last letter descending: $sortedByLast")
                  
                  val numbers = listOf("one", "two", "three", "four")
println("Sorted by length ascending: ${numbers.sortedWith(compareBy { it.length })}")
                
                    let numbers = listOf("one", "two", "three", "four")
print("Sorted by length ascending: ${numbers.sortedWith(compareBy { it.length })}")
                  

Reverse order

                  val numbers = listOf("one", "two", "three", "four")
println(numbers.reversed())
                
                    let numbers = listOf("one", "two", "three", "four")
print(numbers.reversed())
                  
                  val numbers = listOf("one", "two", "three", "four")
val reversedNumbers = numbers.asReversed()
println(reversedNumbers)
                
                    let numbers = listOf("one", "two", "three", "four")
let reversedNumbers = numbers.asReversed()
print(reversedNumbers)
                  
                  val numbers = mutableListOf("one", "two", "three", "four")
val reversedNumbers = numbers.asReversed()
println(reversedNumbers)
numbers.add("five")
println(reversedNumbers)
                
                    let numbers = mutableListOf("one", "two", "three", "four")
let reversedNumbers = numbers.asReversed()
print(reversedNumbers)
numbers.add("five")
print(reversedNumbers)
                  

Random order

                  val numbers = listOf("one", "two", "three", "four")
println(numbers.shuffled())
                
                    let numbers = listOf("one", "two", "three", "four")
print(numbers.shuffled())