Created
March 21, 2013 00:07
-
-
Save wlue/5209648 to your computer and use it in GitHub Desktop.
MappedOrdering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Defines an ordering for a list of keys of type K given an implicit ordering | |
* on value V and a mapping from K to V | |
*/ | |
object MappedOrdering { | |
def withMapping[K, V](mapping: K => V)(implicit ordering: Ordering[V]) = { | |
new MappedOrdering[K, V](mapping)(ordering) | |
} | |
} | |
class MappedOrdering[K, V](mapping: K => V)(implicit ordering: Ordering[V]) extends Ordering[K] { | |
override def compare(first: K, second: K) = { | |
ordering.compare(mapping(first), mapping(second)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment