sortby降序java,F#Seq.sortBy降序排列

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.

解决方案

Looking at the other answers, beware unary minus and MININT:

let a = [| 1; -1; System.Int32.MinValue; 0; System.Int32.MaxValue; 1 |]

printfn "%A" (a |> Array.sortBy (fun x -> x))

// [|-2147483648; -1; 0; 1; 1; 2147483647|]

printfn "%A" (a |> Array.sortBy (fun x -> -x)) // uh-oh!

// [|-2147483648; 2147483647; 1; 1; 0; -1|]

I think you actually want negative-x-minus-one:

printfn "%A" (a |> Array.sortBy (fun x -> -x - 1))

// [|2147483647; 1; 1; 0; -1; -2147483648|]

for a wraparound integer type that spans -2^N..2^N-1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值