From 73adb8524d5d4c2ba0768cd16ae763db1652acc4 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 27 Feb 2017 16:23:19 +0300 Subject: [PATCH] stdlib: Add mutableList.sort function --- .../kotlin/collections/MutableCollections.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/collections/MutableCollections.kt b/runtime/src/main/kotlin/kotlin/collections/MutableCollections.kt index 4a934ea582e..6de07232967 100644 --- a/runtime/src/main/kotlin/kotlin/collections/MutableCollections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/MutableCollections.kt @@ -258,17 +258,17 @@ private fun MutableCollection<*>.retainNothing(): Boolean { /** * Sorts elements in the list in-place according to their natural sort order. */ -@FixmeSorting -public fun > MutableList.sort(): Unit { - TODO() - //if (size > 1) java.util.Collections.sort(this) -} +public fun > MutableList.sort(): Unit = sortWith(Comparator { a: T, b: T -> a.compareTo(b) }) /** * Sorts elements in the list in-place according to the order specified with [comparator]. */ -@FixmeSorting public fun MutableList.sortWith(comparator: Comparator): Unit { - TODO() - //if (size > 1) java.util.Collections.sort(this, comparator) + if (size > 1) { + val it = listIterator() + for (v in toTypedArray().apply { sortWith(comparator) }) { + it.next() + it.set(v) + } + } }