キー値でソートできるように、辞書をシーケンスに「変換」するにはどうすればよいですか?
let results = new Dictionary() results.Add("ジョージ", 10) results.Add("ピーター", 5) results.Add("ジミー", 9) results.Add("ジョン", 2) ランキングを= 結果 ????????? |> Seq.Sort ?????? |> Seq.iter (fun x -> (... 何らかの関数 ...))
キー値でソートできるように、辞書をシーケンスに「変換」するにはどうすればよいですか?
let results = new Dictionary() results.Add("ジョージ", 10) results.Add("ピーター", 5) results.Add("ジミー", 9) results.Add("ジョン", 2) ランキングを= 結果 ????????? |> Seq.Sort ?????? |> Seq.iter (fun x -> (... 何らかの関数 ...))
System.Collections.Dictionary<K,V> は IEnumerable<KeyValuePair<K,V>> であり、F# アクティブ パターン 'KeyValue' は KeyValuePair オブジェクトを分割するのに役立ちます。
open System.Collections.Generic
let results = new Dictionary<string,int>()
results.Add("George", 10)
results.Add("Peter", 5)
results.Add("Jimmy", 9)
results.Add("John", 2)
results
|> Seq.sortBy (fun (KeyValue(k,v)) -> k)
|> Seq.iter (fun (KeyValue(k,v)) -> printfn "%s: %d" k v)