Thursday, May 5, 2011

OrderBy ThenBy in F#

Hello,

Is there any function in F# similar to LINQ fluent syntax for sorting by multiple expressions:

myList.OrderBy(fun x->x.Something).ThenBy(fun x->x.SomethingElse)

I'd love something like:

myList 
|> Seq.sort_by(fun x->x.Something) 
|> Seq.then_by(fun x->x.SomethingElse)

Thx

From stackoverflow
  • You may find some of the sort algorithms here helpful, as I don't know of a wait in F# to do sorting, besides using the .NET functionality.

    http://cs.hubfs.net/forums/thread/3876.aspx

  • Use a tuple as your sort key:

    myList |> Seq.sort_by (fun x -> x.Something, x.SomethingElse)
    
    Brian : Right, tuples sort in lexicographical order, so putting multiple keys in order left-to-right in a tuple does just what is desired.

0 comments:

Post a Comment