LeetCode 1122. Relative Sort Array in F#

URL

https://leetcode.com/problems/relative-sort-array/description/?envType=daily-question&envId=2024-06-11

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202406/relative_sort_array/main.fsx

let relativeSortArray (arr1: int list) (arr2: int list) : int list =
    let m =
        arr2 |> List.indexed |> List.fold (fun acc (i, n) -> Map.add n i acc) Map.empty

    arr1
    |> List.sortWith (fun a b ->
        match Map.tryFind a m, Map.tryFind b m with
        | None, None -> compare a b
        | Some(_), None -> -1
        | None, Some(_) -> 1
        | Some(i), Some(j) -> compare i j)