Sort stability: Difference between revisions

Added Kotlin
(+Stata)
(Added Kotlin)
Line 392:
("UK","London")
("US","New York")
</pre>
 
=={{header|Kotlin}}==
The collections in Kotlin's standard library are thin wrappers around the corresponding JDK collections and, since the latter's sort methods are stable, so too are Kotlin's standard sort functions.
<lang scala>// version 1.1.51
 
fun main(args: Array<String>) {
val cities = listOf("UK London", "US New York", "US Birmingham", "UK Birmingham")
println("Original : $cities")
// sort by country
println("By country : ${cities.sortedBy { it.take(2) } }")
// sort by city
println("By city : ${cities.sortedBy { it.drop(3) } }")
}</lang>
 
{{out}}
<pre>
Original : [UK London, US New York, US Birmingham, UK Birmingham]
By country : [UK London, UK Birmingham, US New York, US Birmingham]
By city : [US Birmingham, UK Birmingham, UK London, US New York]
</pre>
 
9,482

edits