diff options
author | Paul Phillips <paulp@improving.org> | 2010-02-02 20:13:28 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-02-02 20:13:28 +0000 |
commit | 9f55ad82d17f9b0d441ffcfd26b5fc8a817b64f8 (patch) | |
tree | 849a116cd43a5539f6f90eb755b7056f0293a23d | |
parent | 4aeae5f9c7aea8874965539f730e9abaa077729d (diff) | |
download | scala-9f55ad82d17f9b0d441ffcfd26b5fc8a817b64f8.tar.gz scala-9f55ad82d17f9b0d441ffcfd26b5fc8a817b64f8.tar.bz2 scala-9f55ad82d17f9b0d441ffcfd26b5fc8a817b64f8.zip |
It was pointed out that sorted and the 1-arg ve...
It was pointed out that sorted and the 1-arg version of sortWith are the
same method, one with implicit argument, one without. Since sortWith has
never exist in a release, we can un-overload it (which is a win anyway)
and route everything through sorted. Review by moors.
-rw-r--r-- | src/library/scala/collection/SeqLike.scala | 58 |
1 files changed, 22 insertions, 36 deletions
diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index dde7b49175..5b866e5b3d 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -765,32 +765,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => * List("Bob", "John", "Steve", "Tom") * }}} */ - def sortWith(lt: (A, A) => Boolean): Repr = sortWith(Ordering fromLessThan lt) - - /** Sorts this $coll according to an Ordering. - * - * The sort is stable. That is, elements that are equal wrt `lt` appear in the - * same order in the sorted sequence as in the original. - * - * @see scala.math.Ordering - * - * @param ord the ordering to be used to compare elements. - * @return a $coll consisting of the elements of this $coll - * sorted according to the ordering `ord`. - */ - def sortWith[B >: A](ord: Ordering[B]): Repr = { - val arr = new GenericArray[A](this.length) - var i = 0 - for (x <- this) { - arr(i) = x - i += 1 - } - java.util.Arrays.sort( - arr.array, ord.asInstanceOf[Ordering[Object]]) - val b = newBuilder - for (x <- arr) b += x - b.result - } + def sortWith(lt: (A, A) => Boolean): Repr = sorted(Ordering fromLessThan lt) /** Sorts this $Coll according to the Ordering which results from transforming * an implicitly given Ordering with a transformation function. @@ -812,20 +787,31 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => * res0: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped) * }}} */ - def sortBy[B](f: A => B)(implicit ord: Ordering[B]): Repr = sortWith(ord on f) + def sortBy[B](f: A => B)(implicit ord: Ordering[B]): Repr = sorted(ord on f) - /** Sorts this $Coll according to the implicitly given Ordering ord. - * For a $Coll[A] coll, it is equivalent to either of: + /** Sorts this $coll according to an Ordering. * - * coll.sortBy(identity[A]) - * coll.sortWith(_ < _) + * The sort is stable. That is, elements that are equal wrt `lt` appear in the + * same order in the sorted sequence as in the original. * - * @param ord the ordering assumed on domain `B`. - * @return a $coll consisting of the elements of this $coll - * sorted according to the ordering where `x < y` if - * `ord.lt(f(x), f(y))`. + * @see scala.math.Ordering + * + * @param ord the ordering to be used to compare elements. + * @return a $coll consisting of the elements of this $coll + * sorted according to the ordering `ord`. */ - def sorted[B >: A](implicit ord: Ordering[B]): Repr = sortWith(ord) + def sorted[B >: A](implicit ord: Ordering[B]): Repr = { + val arr = new GenericArray[A](this.length) + var i = 0 + for (x <- this) { + arr(i) = x + i += 1 + } + java.util.Arrays.sort(arr.array, ord.asInstanceOf[Ordering[Object]]) + val b = newBuilder + for (x <- arr) b += x + b.result + } /** Converts this $coll to a sequence. * $willNotTerminateInf |