代码之家  ›  专栏  ›  技术社区  ›  Mark Pearl

按降序排序

f#
  •  22
  • Mark Pearl  · 技术社区  · 16 年前

    例如,示例代码是。。。

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

    8 回复  |  直到 16 年前
        1
  •  21
  •   gradbot    16 年前

    看看其他答案,小心一元减号和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|]
    

    我想你真的想要负-x-负-1:

    printfn "%A" (a |> Array.sortBy (fun x -> -x - 1))
    // [|2147483647; 1; 1; 0; -1; -2147483648|]
    

    -2^N..2^N-1 .

        2
  •  24
  •   Patrick McDonald    11 年前

    推出F#4.0(Visual Studio 2015) Seq.sortByDescending Seq.sortDescending

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

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

        3
  •  21
  •   Mau    16 年前

    seq { 0..10 } 
        |> Seq.sortBy (~-)    // Unary minus
        |> Seq.iter (printfn "%d")
    
        4
  •  18
  •   Stephen Swensen    16 年前

    首先,让我们扩展一下 Seq 用一个 sortWith 函数与列表和数组相同。

    namespace Microsoft.FSharp.Collections
    module Seq =
        let sortWith f e = 
            let e' = e |> Seq.toArray
            e' |> Array.sortInPlaceWith f
            e' |> Seq.readonly
    

    Operators 有一个经常有用的 flip 功能。

    namespace Microsoft.FSharp.Core
    module Operators =
        let flip f x y = f y x
    

    compare 函数用于泛型(您可以将其用于任何可比较元素的序列)和安全(根据Brian的观察)逆序排序。

    {0..10}
    |> Seq.sortWith (flip compare)
    |> Seq.iter (printfn "%A")
    
        5
  •  9
  •   dahlbyk    16 年前

    另一种选择是包装 System.Linq.Enumerable.OrderByDescending()

    // #r "System.Core"
    module Seq =
        let sortByDesc f s = Enumerable.OrderByDescending(s, new Func<'a, 'b>(f))
    
    {0..10} |> Seq.sortByDesc (fun x -> x)
    
        6
  •  7
  •   JaredPar    16 年前

    您可以通过提供一个负键来解决这个问题

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

    而且使用 printf 在F#中显示文本的函数。例如

    let DisplayList = 
      seq { 0..10 } 
      |> Seq.sortBy (fun x -> -x)
      |> Seq.iter (printfn "%d")
    
        7
  •  6
  •   pblasucci    16 年前

    如果你提前知道,你会有一个相对较小的序列,我认为这是更可读的。。。

    let x = seq { 0.. 10 } |> Seq.toArray |> Array.rev

        8
  •  5
  •   user612566 user612566    13 年前

    使用一元减号的解决方案: (fun x -> -x - 1) (fun x -> -x)

    let a = [| 0uy; 255uy; 254uy; 1uy |]
    printfn "%A" (a |> Array.sortBy (fun x -> -x - 1))
    // error FS0001: The type 'byte' does not support the operator '~-'
    

    相反,我们可以利用 -x = ~~~x + 1 哪里 ~~~ -x - 1 = ~~~x . 因此,适用于有符号和无符号类型的简短解决方案是:

    Array.sortBy (~~~) // equivalent to Array.sortBy (fun x -> ~~~x)
    

    示例:

    let a = [| 0uy; 255uy; 254uy; 1uy |]
    printfn "%A" (a |> Array.sortBy (~~~))
    // [|255uy; 254uy; 1uy; 0uy|]
    let a = [| 1; -1; System.Int32.MinValue; 0; System.Int32.MaxValue; 1 |]
    printfn "%A" (a |> Array.sortBy (~~~))
    // [|2147483647; 1; 1; 0; -1; -2147483648|]