Array.sortWith<'T> — Funkcja (F#)

Sortuje elementy tablicy przy użyciu funkcji danego porównania jako zamówienie, zwracanie nowej tablicy.

Ścieżka obszaru nazw/modułu: Microsoft.FSharp.Collections.Array

Zestaw: FSharp.Core (w FSharp.Core.dll)

// Signature:
Array.sortWith : ('T -> 'T -> int) -> 'T [] -> 'T []

// Usage:
Array.sortWith comparer array

Parametry

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

    Funkcja do porównywania par elementów tablicy.

  • array
    Type: 'T[]

    Tablicy wejściowy.

Wartość zwracana

Tablicy posortowane.

Uwagi

Nie jest stabilne sortowania, czyli oryginalnego zamówienia równe elementy mogą być utracone.Stabilne sortowania, należy rozważyć użycie Seq.sort.

Ta funkcja o nazwie SortWith w skompilowane zestawy.Jeżeli języka, niż F# lub przez odbicie, uzyskują dostęp do funkcji, należy użyć tej nazwy.

Przykład

Poniższy kod ilustruje użycie Array.sortWith.

open System

let array1 = [| "<>"; "&"; "&&"; "&&&"; "<"; ">"; "|"; "||"; "|||" |]
printfn "Before sorting: "
array1 |> 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)

Array.sortWith sortFunction array1
|> printfn "After sorting: \n%A"

Dane wyjściowe

  

Platformy

Windows 8, Windows 7, Windows Server 2012 Windows Server 2008 R2

Informacje o wersji

F# Core wersji biblioteki

Obsługiwane: 2.0, 4.0, przenośne

Zobacz też

Informacje

Collections.Array — Moduł (F#)

Microsoft.FSharp.Collections — Przestrzeń nazw (F#)