Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Seq.sortBy in descending order

Tags:

I am fairly new to F# and came by the Seq.sortBy function however it is sorting my list in ascending order. How do I get it to sort in descending order using the Seq.sort?

For instance an example code would be...

let DisplayList = seq{0..10} |> Seq.sortBy(fun x -> x) |> Seq.iter(fun x -> Console.WriteLine(x.ToString())) 

gives me an output of 1 2 3 4 5 6 7 8 9 10, when I really want it to do it from 10 to 1.

like image 951
Mark Pearl Avatar asked Jun 24 '10 15:06

Mark Pearl


1 Answers

F# 4.0 (Visual Studio 2015) introduced Seq.sortByDescending and Seq.sortDescending

let DisplayList =     seq { 0..10 }     |> Seq.sortDescending         ' or |> Seq.sortByDescending id     |> Seq.iter Console.WriteLine 

See https://github.com/Microsoft/visualfsharp/wiki/F%23-4.0-Status and https://github.com/fsharp/FSharpLangDesign/blob/master/FSharp-4.0/ListSeqArrayAdditions.md

like image 158
Patrick McDonald Avatar answered Nov 28 '22 02:11

Patrick McDonald