F# multi-key record sort with null ordering
F#
type Person = { Name: string; Age: int }
let compareNullFirst (a: string) (b: string) =
match isNull a, isNull b with
| true, true -> 0
| true, false -> -1
| false, true -> 1
| _ -> System.String.CompareOrdinal(a, b)
// List.sortWith is stable, so equal keys keep input order
let sortPeople (people: Person list) =
people
|> List.sortWith (fun x y ->
match compareNullFirst x.Name y.Name with
| 0 -> compare x.Age y.Age
| c -> c)
let sorted = sortPeople [ { Name = "ada"; Age = 36 } ]