tag:blogger.com,1999:blog-14415229.post2000263292236061193..comments2023-10-10T16:55:02.139+02:00Comments on Chase The Devil: Scala is MadFabienhttp://www.blogger.com/profile/07288327695801480778noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-14415229.post-45619289331862037652012-12-13T14:28:13.427+01:002012-12-13T14:28:13.427+01:00Yes. Instead of extending Ordered[Point], you can ...Yes. Instead of extending Ordered[Point], you can provide implicit Ordering of desired type in the companion object:<br><br>object Point {<br> implicit def pointOrdering[T <: Point] = new Ordering[T] {<br> def compare(t1: T, t2: T) {<br> return math.signum(t1.value - t2.value).toInt<br> }<br> }<br>}<br><br>This should work. There is still a slight problem that a new ordering object will be created for every sort call, and I don't see a way to avoid it without a cast :(Alexey Romanovhttps://www.blogger.com/profile/04414745317669007614noreply@blogger.comtag:blogger.com,1999:blog-14415229.post-73443815069744574312012-12-13T13:08:21.782+01:002012-12-13T13:08:21.782+01:00Thanks Alexey, T :< Point : ClassManifest : Ord...Thanks Alexey, T :< Point : ClassManifest : Ordering works.Fabienhttps://www.blogger.com/profile/07288327695801480778noreply@blogger.comtag:blogger.com,1999:blog-14415229.post-78171732620900061842012-12-12T22:50:33.961+01:002012-12-12T22:50:33.961+01:00Ah, I see now. The problem isn't with ClassMan...Ah, I see now. The problem isn't with ClassManifest, but with scala.math.Ordering. You have an Ordering[Point], but you <i>don't</i> have an Ordering[T] (same as in Java, a Comparator<Point> isn't a Comparator<T>). [T : ClassManifest : Ordering] should work. Arrays.sort works because it's willing to blow up at the runtime if elements of the array turn out not to be comparable.Alexey Romanovhttps://www.blogger.com/profile/04414745317669007614noreply@blogger.comtag:blogger.com,1999:blog-14415229.post-7810375410022082642012-12-12T16:09:14.859+01:002012-12-12T16:09:14.859+01:00I have added the actual code. points.sortWith woul...I have added the actual code. points.sortWith would work, but I would like to just use the default compare method.<br>points.sortBy has the same issue as Sorting.quickSort(points). I could not find a solution without explicit casting.Fabienhttps://www.blogger.com/profile/07288327695801480778noreply@blogger.comtag:blogger.com,1999:blog-14415229.post-1651910362636570612012-12-09T15:41:18.430+01:002012-12-09T15:41:18.430+01:00You should only need ClassManifest when doing thin...You should only need ClassManifest when doing things like the equivalent of `new T[length]`, which you can't do in Java. In particular, scala.util.Sorting.quickSort[K](a: Array[K])(implicit arg0: Ordering[K]) doesn't need it (it sorts an existing array in-place).Alexey Romanovhttps://www.blogger.com/profile/04414745317669007614noreply@blogger.comtag:blogger.com,1999:blog-14415229.post-61780174410854394982012-12-07T17:19:00.190+01:002012-12-07T17:19:00.190+01:00Why not use points.sortBy(identity)or (for more fl...Why not use <br><br>points.sortBy(identity)<br><br>or (for more flexibility or if the type of the Points is not Ordered) <br><br>points.sortWith{(a,b) => yourBooleanComparisionHere(a,b)}<br><br>These methods work on any Seq. <br><br>For arrays, there is also an in-place sort as (Ordered version):<br><br>util.Sorting.quickSort(points)<br><br>Henrynoreply@blogger.com