共用方式為


List.sortWith<'T> 函式 (F#)

更新:2010 年 8 月

使用指定的比較函式排序指定的清單。

命名空間/模組路徑: Microsoft.FSharp.Collections.List

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
List.sortWith : ('T -> 'T -> int) -> 'T list -> 'T list

// Usage:
List.sortWith comparer list

參數

  • comparer
    Type: 'T -> 'T -> int

    用來比較清單項目的函式。

  • list
    Type: 'T list

    輸入清單。

傳回值

已排序的清單。

備註

這是穩定的排序,也就是保留原先的相等的項目順序。

這個函式是名為 SortWith中 已編譯的組件。 如果從一個語言,F # 以外,或透過反映存取函式使用這個名稱。

範例

下列程式碼顯示如何使用 List.sortWith

open System

let list1 = [ "<>"; "&"; "&&"; "&&&"; "<"; ">"; "|"; "||"; "|||" ]
printfn "Before sorting: "
list1 |> printfn "%A"
let sortFunction (string1:string) (string2:string) =
    if (string1.Length > string2.Length) then
       1
    else if (string1.Length < string2.Length) then
       -1
    else
        String.Compare(string1, string2)
List.sortWith sortFunction list1
|> printfn "After sorting:\n%A"

輸出

  

平台

Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

版本資訊

F# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Collections.List 模組 (F#)

Microsoft.FSharp.Collections 命名空間 (F#)

變更記錄

日期

History

原因

2010 年 8 月

加入程式碼範例。

資訊加強。