Order two numerical lists: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Slight simplification) |
(Added Kotlin) |
||
Line 804: | Line 804: | ||
[2015,5] is not lexically prior to |
[2015,5] is not lexically prior to |
||
[-7.0710678118654755,2500.0] |
[-7.0710678118654755,2500.0] |
||
</pre> |
|||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.0.6 |
|||
operator fun <T> List<T>.compareTo(other: List<T>): Int |
|||
where T: Comparable<T>, T: Number { |
|||
for (i in 0 until this.size) { |
|||
if (other.size == i) return 1 |
|||
when { |
|||
this[i] < other[i] -> return -1 |
|||
this[i] > other[i] -> return 1 |
|||
} |
|||
} |
|||
return if (this.size == other.size) 0 else -1 |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val list1 = listOf(1, 2, 3, 4, 5) |
|||
val list2 = listOf(1, 2, 1, 5, 2, 2) |
|||
val list3 = listOf(1, 2, 1, 5, 2) |
|||
val list4 = listOf(1, 2, 1, 5, 2) |
|||
val list5 = listOf(1, 2, 1, 3, 2) |
|||
val list6 = listOf(1, 2, 0, 4, 4, 0, 0, 0) |
|||
val list7 = listOf(1, 2, 0, 4, 4, 1, 0, 0) |
|||
println("list1 : $list1") |
|||
println("list2 : $list2") |
|||
println("list3 : $list3") |
|||
println("list4 : $list4") |
|||
println("list5 : $list5") |
|||
println("list6 : $list6") |
|||
println("list7 : $list7") |
|||
println() |
|||
println("list1 > list2 = ${list1 > list2}") |
|||
println("list2 > list3 = ${list2 > list3}") |
|||
println("list3 > list4 = ${list3 > list4}") |
|||
println("list4 > list5 = ${list4 > list5}") |
|||
println("list5 > list6 = ${list5 > list6}") |
|||
println("list6 > list7 = ${list6 > list7}") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
list1 : [1, 2, 3, 4, 5] |
|||
list2 : [1, 2, 1, 5, 2, 2] |
|||
list3 : [1, 2, 1, 5, 2] |
|||
list4 : [1, 2, 1, 5, 2] |
|||
list5 : [1, 2, 1, 3, 2] |
|||
list6 : [1, 2, 0, 4, 4, 0, 0, 0] |
|||
list7 : [1, 2, 0, 4, 4, 1, 0, 0] |
|||
list1 > list2 = true |
|||
list2 > list3 = true |
|||
list3 > list4 = false |
|||
list4 > list5 = true |
|||
list5 > list6 = true |
|||
list6 > list7 = false |
|||
</pre> |
</pre> |
||