Feb. 24th, 2011

juan_gandhi: (Default)
Spent two hours studying this.



Somehow we felt extremely happy after finishing this.
juan_gandhi: (Default)
A guy asks a question on a scala forum:


In C# (gasp!) they have this nice feature:

items.orderBy( item => item.foo ).thenBy ( item => item.bar )

Is there a good way to do this in Scala? E.g. sort my items first by
"foo", if they tie on "foo" then sort them by "bar"?

I end up writing this code:
items.sort { case (item1,item2) =>
 if ( item1.foo == item2.foo )
   item1.bar > item2.bar
 else
   item1.foo > item2.foo
}

which gets much worse if you have a third sort key.


and the answer is:


On Thu, Feb 24, 2011 at 3:49 PM, Alex Cruise <alex@******.com> wrote:
Tuples are also comparable in the expected way, so you can also do this:
items sortBy { x => (x.bar, x.foo) }

-0xe1a

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78910111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 9th, 2025 04:13 pm
Powered by Dreamwidth Studios