Fix functions List<T>.sort() and add sort functions for java.lang.Iterables.
See Problem 3 at https://github.com/JetBrains/kotlin/pull/78#issuecomment-6533534
This commit is contained in:
@@ -105,6 +105,18 @@ public fun <T> java.lang.Iterable<T>.withIndices() : java.lang.Iterable<#(Int, T
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.sort() : List<T> {
|
||||
val list = toList()
|
||||
java.util.Collections.sort(list)
|
||||
return list
|
||||
}
|
||||
|
||||
public inline fun <in T> java.lang.Iterable<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
|
||||
val list = toList()
|
||||
java.util.Collections.sort(list, comparator)
|
||||
return list
|
||||
}
|
||||
|
||||
private class NumberedIterator<TT>(private val sourceIterator : java.util.Iterator<TT>) : java.util.Iterator<#(Int, TT)> {
|
||||
private var nextIndex = 0
|
||||
|
||||
|
||||
@@ -40,17 +40,6 @@ public inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSorted
|
||||
|
||||
// List APIs
|
||||
|
||||
|
||||
public inline fun <in T: java.lang.Comparable<T>> List<T>.sort() : List<T> {
|
||||
Collections.sort(this)
|
||||
return this
|
||||
}
|
||||
|
||||
public inline fun <in T> List<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
|
||||
Collections.sort(this, comparator)
|
||||
return this
|
||||
}
|
||||
|
||||
/** Returns the List if its not null otherwise returns the empty list */
|
||||
public inline fun <T> java.util.List<T>?.orEmpty() : java.util.List<T>
|
||||
= if (this != null) this else Collections.emptyList<T>() as java.util.List<T>
|
||||
|
||||
Reference in New Issue
Block a user