简体   繁体   English

F#排序数组

[英]F# sorting array

I have an array like this, 我有一个像这样的数组

[|{Name = "000016.SZ";
     turnover = 3191591006.0;
     MV = 34462194.8;}; 
  {Name = "000019.SZ";
     turnover = 2316868899.0;
     MV = 18438461.48;}; 
  {Name = "000020.SZ";
     turnover = 1268882399.0;
     MV = 7392964.366;};
  .......
    |]

How do I sort this array according to "turnover"? 如何根据“营业额”对该数组进行排序? Thanks (does not have much context to explain the code section? how much context should I write) 谢谢(没有太多上下文可以解释代码部分?我应该写多少上下文)

假设数组在arr您可以执行以下操作

arr |> Array.sortBy (fun t -> t.turnover)

I know this has already been answered beautifully; 我知道这已经得到了很好的回答。 however, I am finding that, like Haskell, F# matches the way I think and thought I'd add this for other novices :) 但是,我发现,就像Haskell一样,F#与我的想法相符,并认为我会将其添加给其他新手:)

let rec sortData = 
  function 
  | [] -> []
  | x :: xs -> 
    let smaller = List.filter (fun e -> e <= x) >> sortData
    let larger = List.filter (fun e -> e > x) >> sortData
    smaller xs @ [ x ] @ larger xs

Note 1: "a >> b" is function composition and means "create a function, f, such that fx = b(a(x))" as in "apply a then apply b" and so on if it continues: a >> b >> c >>... 注1:“ a >> b”是函数组成,表示“创建函数f,以使fx = b(a(x())”,如“应用a然后应用b”中所示,依此类推,如果继续:a >> b >> c >> ...

Note 2: "@" is list concatenation, as in [1..100] = [1..12] @ [13..50] @ [51..89] @ [90..100]. 注意2:“ @”是列表连接,如[1..100] = [1..12] @ [13..50] @ [51..89] @ [90..100]。 This is more powerful but less efficient than cons, "::", which can only add one element at a time and only to the head of a list, a::[b;c;d] = [a;b;c;d] 这比cons“ ::”功能更强大,但效率更低。cons::一次只能添加一个元素,并且只能添加到列表的开头,即a :: [b; c; d] = [a; b; c ; d]

Note 3: the List.filter (fun e ->...) expressions produces a "curried function" version holding the provided filtering lambda. 注意3:List.filter(fun e-> ...)表达式会生成一个“ curried function”版本,其中包含提供的过滤lambda。

Note 4: I could have made "smaller" and "larger" lists instead of functions (as in "xs |> filter |> sort"). 注4:我本可以创建“较小”和“较大”列表,而不是函数(如“ xs |> filter |> sort”)。 My choice to make them functions was arbitrary. 我选择使它们起作用是任意的。

Note 5: The type signature of the sortData function states that it requires and returns a list whose elements support comparison: 注5:sortData函数的类型签名说明它需要并返回其元素支持比较的列表:

_arg1:'a list -> 'a list when 'a : comparison 

Note 6: There is clarity in brevity (despite this particular post :) ) 注意6:简洁明了(尽管有此特定帖子:))

As a testament to the algorithmic clarity of functional languages, the following optimization of the above filter sort is three times faster (as reported by VS Test Explorer). 为了证明功能语言的算法清晰性,上述筛选器排序的以下优化速度提高了三倍(如VS Test Explorer所报道)。 In this case, the list is traversed only once per pivot (the first element) to produce the sub-lists of smaller and larger items. 在这种情况下,每个枢轴(第一个元素)仅遍历该列表一次,以生成较小和较大项目的子列表。 Also, an equivalence list is introduced which collects matching elements away from further comparisons. 而且,引入了等效列表,该列表从进一步的比较中收集匹配元素。

let rec sort3 =
  function
  | [] -> []
  | x::xs ->
      let accum tot y =
        match tot with
        | (a,b,c) when y < x -> (y::a,b,c)
        | (a,b,c) when y = x -> (a,y::b,c)
        | (a,b,c) -> (a,b,y::c)
      let (a,b,c) = List.fold accum ([],[x],[]) xs
      (sort3 a) @ b @ (sort3 c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM