1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 09:41:15 +02:00

libexpr: Document requirements for comparator passed to builtins.sort

This commit is contained in:
Sergei Zimmerman 2025-06-15 16:51:42 +00:00
parent 351d898c43
commit ddcfc81ff1
No known key found for this signature in database
GPG key ID: A9B0B557CA632325

View file

@ -3725,6 +3725,32 @@ static RegisterPrimOp primop_sort({
This is a stable sort: it preserves the relative order of elements
deemed equal by the comparator.
*comparator* must impose a strict weak ordering on the set of values
in the *list*. This means that for any elements *a*, *b* and *c* from the
*list*, *comparator* must satisfy the following relations:
1. Transitivity
```nix
comparator a b && comparator b c -> comparator a c
```
1. Irreflexivity
```nix
comparator a a == false
```
1. Transitivity of equivalence
```nix
let equiv = a: b: (!comparator a b && !comparator b a); in
equiv a b && equiv b c -> equiv a c
```
If the *comparator* violates any of these properties, then `builtins.sort`
reorders elements in an unspecified manner.
)",
.fun = prim_sort,
});