Skip to content

Instantly share code, notes, and snippets.

@wlue
Created March 21, 2013 00:07
Show Gist options
  • Save wlue/5209648 to your computer and use it in GitHub Desktop.
Save wlue/5209648 to your computer and use it in GitHub Desktop.
MappedOrdering
/**
* 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