From 744049bb71e61fbf072ccdeeaae9bc63f127c069 Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Mon, 3 Aug 2009 18:47:21 +0000 Subject: Applied patch for #2145. --- .../collection/generic/SequenceTemplate.scala | 27 ++++++++++++++++++++++ .../collection/generic/TraversableTemplate.scala | 24 ------------------- 2 files changed, 27 insertions(+), 24 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/collection/generic/SequenceTemplate.scala b/src/library/scala/collection/generic/SequenceTemplate.scala index ec08d8a160..9ae4a29770 100644 --- a/src/library/scala/collection/generic/SequenceTemplate.scala +++ b/src/library/scala/collection/generic/SequenceTemplate.scala @@ -578,6 +578,33 @@ trait SequenceTemplate[+A, +This <: IterableTemplate[A, This] with Sequence[A]] */ override def toString = super[IterableTemplate].toString + /** Sort the sequence according to the comparison function + * <(e1: a, e2: a) => Boolean, + * which should be true iff e1 is smaller than + * e2. + * 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 lt the comparison function + * @return a sequence sorted according to the comparison function + * <(e1: a, e2: a) => Boolean. + * @ex
+   *    List("Steve", "Tom", "John", "Bob")
+   *      .sortWith((e1, e2) => (e1 compareTo e2) < 0) =
+   *    List("Bob", "John", "Steve", "Tom")
+ */ + def sortWith(lt: (A, A) => Boolean): This = { + val arr = toArray + import java.util.{Arrays, Comparator} + Arrays.sort(arr, new Comparator[A] { + override def compare(a: A, b: A) = + if (lt(a, b)) -1 else if (lt(b, a)) 1 else 0 + }) + val b = newBuilder + for (x <- arr) b += x + b.result + } + /** Returns index of the last element satisying a predicate, or -1. */ @deprecated("use `lastIndexWhere' instead") def findLastIndexOf(p: A => Boolean): Int = lastIndexWhere(p) diff --git a/src/library/scala/collection/generic/TraversableTemplate.scala b/src/library/scala/collection/generic/TraversableTemplate.scala index 6e2c32e251..79e050b976 100644 --- a/src/library/scala/collection/generic/TraversableTemplate.scala +++ b/src/library/scala/collection/generic/TraversableTemplate.scala @@ -716,30 +716,6 @@ self => @experimental def toSet[B >: A]: immutable.Set[B] = immutable.Set() ++ thisCollection - /** Sort the traversable according to the comparison function - * <(e1: a, e2: a) => Boolean, - * which should be true iff e1 is smaller than - * e2. - * The sort is stable. That is elements that are equal wrt `lt` appear in the - * same order in the sorted traversable as in the original. - * - * @param lt the comparison function - * @return a traversable sorted according to the comparison function - * <(e1: a, e2: a) => Boolean. - * @ex
-   *    List("Steve", "Tom", "John", "Bob")
-   *      .sort((e1, e2) => (e1 compareTo e2) < 0) =
-   *    List("Bob", "John", "Steve", "Tom")
- * !!! - def sortWith(lt : (A,A) => Boolean): This = { - val arr = toArray - Array.sortWith(arr, lt) - val b = newBuilder[A] - for (x <- arr) b += x - b.result - } - */ - /** Returns a string representation of this traversable object. The resulting string * begins with the string start and is finished by the string * end. Inside, the string representations of elements (w.r.t. -- cgit v1.2.3