summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/Ordering.scala5
-rw-r--r--src/library/scala/collection/SeqLike.scala27
2 files changed, 28 insertions, 4 deletions
diff --git a/src/library/scala/Ordering.scala b/src/library/scala/Ordering.scala
index 8b4db3f549..4a90a03dfb 100644
--- a/src/library/scala/Ordering.scala
+++ b/src/library/scala/Ordering.scala
@@ -92,6 +92,11 @@ trait Ordering[T] extends Comparator[T] with PartialOrdering[T] {
def compare(x: T, y: T) = outer.compare(y, x)
}
+ /** Given a function mapping U => T, creates Ordering[U]. */
+ def map[U](f: U => T): Ordering[U] = new Ordering[U] {
+ def compare(x: U, y: U) = outer.compare(f(x), f(y))
+ }
+
class Ops(lhs: T) {
def <(rhs: T) = lt(lhs, rhs)
def <=(rhs: T) = lteq(lhs, rhs)
diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala
index 9f80386b2a..a551a0b961 100644
--- a/src/library/scala/collection/SeqLike.scala
+++ b/src/library/scala/collection/SeqLike.scala
@@ -12,6 +12,7 @@
package scala.collection
import generic._
import mutable.{ListBuffer, HashMap, GenericArray}
+import annotation.experimental
// import immutable.{List, Nil, ::}
import generic._
@@ -207,7 +208,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
def findIndexOf(p: A => Boolean): Int = indexWhere(p)
/** Returns the index of the first occurence of the specified
- * object in this iterable object.
+ * object in this sequence.
*
* @note may not terminate for infinite-sized collections.
* @param elem element to search for.
@@ -218,7 +219,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
def indexOf[B >: A](elem: B): Int = indexOf(elem, 0)
/** Returns the index of the first occurence of the specified
- * object in this iterable object, starting from a start index, or
+ * object in this sequence, starting from a start index, or
* -1, if none exists.
*
* @note may not terminate for infinite-sized collections.
@@ -530,7 +531,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
b.result
}
- /** Sort the iterable according to the comparison function
+ /** Sort the sequence according to the comparison function
* <code>&lt;(e1: a, e2: a) =&gt; Boolean</code>,
* which should be true iff <code>e1</code> is smaller than
* <code>e2</code>.
@@ -538,7 +539,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
* same order in the sorted sequence as in the original.
*
* @param lt the comparison function
- * @return an iterable sorted according to the comparison function
+ * @return a sequence sorted according to the comparison function
* <code>&lt;(e1: a, e2: a) =&gt; Boolean</code>.
* @ex <pre>
* List("Steve", "Tom", "John", "Bob")
@@ -561,6 +562,24 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
b.result
}
+ /** Sort the sequence according to the Ordering which results from mapping
+ * the implicitly given Ordering[B] to an Ordering[A]. For example:
+ *
+ * <code>
+ * val words = "The quick brown fox jumped over the lazy dog".split(' ')
+ * // this works because scala.Ordering will implicitly provide an Ordering[Tuple2[Int, Char]]
+ * words.sortBy(x => (x.length, x.head.toLower))
+ * res0: Array[String] = Array(dog, fox, The, the, lazy, over, brown, quick, jumped)
+ * </code>
+ *
+ * @param f the mapping function
+ * @param ord the Ordering[B]
+ * @return the sorted representation
+ */
+
+ @experimental
+ def sortBy[B](f: A => B)(implicit ord: Ordering[B]): Repr = sortWith(ord map f)
+
/**
* Overridden for efficiency.
*