简体   繁体   English

如何在F#中订购LIST

[英]How to order a LIST in F#

Total F# n00b question. 总F#n00b问题。 How do I sort a LIST data structure? 如何对LIST数据结构进行排序?

Edit: Sorry, my data structure is actually a LIST. 编辑:对不起,我的数据结构实际上是一个列表。

maybe i should add my code since just using ".sort" hasn't worked: 也许我应该添加我的代码,因为只是使用“.sort”没有工作:

let getDataFromDb (db: MyDB) Id =
Query.query <@ seq { 
    big honking database/FLinq query
    yield  (sec, pm, sr, trade, tradeRec, i, pm_firm, files, lt)
} @> |> List.ofSeq

when I change the last line of code to this: 当我将最后一行代码更改为:
} @> |> List.ofSeq.sortBy fst } @> |> List.ofSeq.sortBy fst
I get the following: 我得到以下内容:

Error 1 The field, constructor or member 'sortBy' is not defined 错误1未定义字段,构造函数或成员“sortBy”

ugh, what a pain. 呃,多么痛苦。 I'm trying this now: 我现在正在尝试这个:

|> List.ofSeq |> List.sortBy

But I'm getting this: 但是我得到了这个:

Error 1 Type mismatch. 错误1类型不匹配。 Expecting a (Security * RoleContributor * RoleContributor * SuggestedTrade * SuggestedTradeRecommendation * Idea * RoleContributor * SupportingUploadedFile * LargeText) list -> 'a but given a ('b -> 'c) -> 'b list -> 'b list The type '(Security * RoleContributor * RoleContributor * SuggestedTrade * SuggestedTradeRecommendation * Idea * RoleContributor * SupportingUploadedFile * LargeText) list' does not match the type ''a -> 'b' 期待a(Security * RoleContributor * RoleContributor * SuggestedTrade * SuggestedTradeRecommendation * Idea * RoleContributor * SupportingUploadedFile * LargeText)list - >'a a given a('b - >'c) - >'b list - >'b list The type' (安全* RoleContributor * RoleContributor * SuggestedTrade * SuggestedTradeRecommendation * Idea * RoleContributor * SupportingUploadedFile * LargeText)列表'与类型'' - >'b'不匹配

Seq.sortBy would do that. Seq.sortBy会这样做。

However sorting implies you know the key values of the full sequence at the time of sorting, so by definition you cannot use this on infinite sequences. 但是排序意味着您在排序时知道完整序列的关键值,因此根据定义,您无法在无限序列上使用它。

Edit: The equivalent for lists has the same name: List.sortBy 编辑:列表的等效项具有相同的名称: List.sortBy

MSDN example: MSDN示例:

let sortedList2 = List.sortBy (fun elem -> abs elem) [1; 4; 8; -2; 5]
printfn "%A" sortedList2

Edit 2: 编辑2:

From your new example it seems like you have a list of tuples. 从你的新例子看,你似乎有一个元组列表。 Now it depends on what item in the tuple you want to search by. 现在它取决于你想要搜索的元组中的哪个项目。

As others said, Seq.sortBy is the way to go. 正如其他人所说, Seq.sortBy是要走的路。 If you're using FLinq to read some data from a database, then it is a good idea to include the sorting as part of the database query (enclosed in <@ .. @> ) so that the sorting is done on the SQL server: 如果您正在使用FLinq从数据库中读取一些数据,那么最好将排序作为数据库查询的一部分(包含在<@ .. @> ),以便在SQL服务器上完成排序:

let getDataFromDb (db: MyDB) Id = 
  <@ seq { big honking database/FLinq query 
             yield  (sec, pm, sr, trade, tradeRec, i, pm_firm, files, lt)
     |> Seq.sortBy (fun (_, _, _, _, _, i, _, _, _) -> i) @>
  |> List.ofSeq 

To make this a little nicer, you could return tuple containing key and all other elements as nested tuple eg key, (sec, pm, ..., lt) and then just sort using the first element: 为了使这个更好一点,你可以返回包含键的元组和所有其他元素作为嵌套元组,例如key, (sec, pm, ..., lt) ,然后使用第一个元素排序:

 |> Seq.sortBy (fun (k, _) -> k)

(I had some troubles using tuples with LINQ to Entities, but I believe that it should work in LINQ to SQL). (我在使用LINQ to Entities的元组时遇到了一些麻烦,但我相信它应该在LINQ to SQL中工作)。

Use: 采用:

Query.query <@ ... @>
|> List.sortBy (fun (sec, _, _, _, _, _, _, _, _) -> sec)

Note that using tuples with that many elements is really bad style in F#. 请注意,在F#中使用具有那么多元素的元组是非常糟糕的样式。 Use something more structured like a record type to give names to the fields and avoid confusion. 使用更像结构类似记录类型的东西来为字段命名并避免混淆。

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

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