Bei der Gruppe, für die Sie eine Mitteilung verfassen, handelt es sich um eine
Usenet-Gruppe . Wenn Sie in dieser Gruppe Nachrichten posten, ist Ihre E-Mail-Adresse für jeden im Internet sichtbar
Ihre Antwort wurde nicht gesendet.
Die Nachricht wurde übermittelt.
Von:
"Dennis Haupt" <h-s... @gmx.de>
Datum: Thu, 08 Nov 2012 17:35:13 +0100
Lokal: Do 8 Nov. 2012 11:35
Betreff: sortByUncomparableValue
let's assume i have a Comparator[X] and a List[Y]
what i want to do is:
ys.sortWith((y1, y2) => xComp.compare(y1.x, y2.x) > 0)
but i don't like > 0, that is so oldschool and low level :( and i might confuse it with < 0. or i did. see? bad code.
is there a better way?
Sie müssen sich
anmelden , bevor Sie Nachrichten veröffentlichen können.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Von:
Daniel Sobral <dcsob... @gmail.com>
Datum: Thu, 8 Nov 2012 15:27:45 -0200
Lokal: Do 8 Nov. 2012 12:27
Betreff: Re: [scala-user] sortByUncomparableValue
ys.sortBy(_.x)
Sorting takes an Ordering, and there's an implicit conversion from
Comparator to Ordering. So that ought to work.
On Thu, Nov 8, 2012 at 2:35 PM, Dennis Haupt <h-s
... @gmx.de> wrote:
> let's assume i have a Comparator[X] and a List[Y]
> what i want to do is:
> ys.sortWith((y1, y2) => xComp.compare(y1.x, y2.x) > 0)
> but i don't like > 0, that is so oldschool and low level :( and i might
> confuse it with < 0. or i did. see? bad code.
> is there a better way?
--
Daniel C. Sobral
I travel to the future all the time.
Sie müssen sich
anmelden , bevor Sie Nachrichten veröffentlichen können.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Von:
François-Xavier Thomas <fx.tho... @gmail.com>
Datum: Thu, 8 Nov 2012 18:29:11 +0100
Lokal: Do 8 Nov. 2012 12:29
Betreff: Re: [scala-user] sortByUncomparableValue
There's an Ordered[T] trait, so maybe you can make X and Y inherit from Ordered, override the "compare" method, and get every comparison operator for free?
> scala> case class A(value: Int) extends Ordered[A] { def compare(that: A) = value.compare(that.value) }
> defined class A
> scala> val a = new A(1)
> a: A = A(1)
> scala> val b = new A(2)
> b: A = A(2)
> scala> a < b
> res1: Boolean = true
Cheers,
François-Xavier
On Nov 8, 2012, at 5:35 PM, Dennis Haupt wrote:
> let's assume i have a Comparator[X] and a List[Y]
> what i want to do is:
> ys.sortWith((y1, y2) => xComp.compare(y1.x, y2.x) > 0)
> but i don't like > 0, that is so oldschool and low level :( and i might confuse it with < 0. or i did. see? bad code.
> is there a better way?
Sie müssen sich
anmelden , bevor Sie Nachrichten veröffentlichen können.
Sie haben nicht die erforderliche Berechtigung zum Posten.
Von:
François-Xavier Thomas <fx.tho... @gmail.com>
Datum: Thu, 8 Nov 2012 18:32:28 +0100
Lokal: Do 8 Nov. 2012 12:32
Betreff: Re: [scala-user] sortByUncomparableValue
Ah, didn't notice your mail in time Daniel!
Thanks for the tip anyway, I didn't know Comparator was implicitly converted to Ordering.
Cheers,
François-Xavier
On Nov 8, 2012, at 6:27 PM, Daniel Sobral wrote:
> ys.sortBy(_.x)
> Sorting takes an Ordering, and there's an implicit conversion from Comparator to Ordering. So that ought to work.
Sie müssen sich
anmelden , bevor Sie Nachrichten veröffentlichen können.
Sie haben nicht die erforderliche Berechtigung zum Posten.