doc: improve lib.compareLists documentation

This commit is contained in:
Johannes Kirschbauer 2024-12-26 16:12:28 +01:00
parent 08de518f84
commit 477b98b2a9
No known key found for this signature in database

View file

@ -1330,35 +1330,48 @@ rec {
pairs); pairs);
/** /**
Compare two lists element-by-element. Compare two lists element-by-element with a comparison function `cmp`.
List elements are compared pairwise in order by the provided comparison function `cmp`,
the first non-equal pair of elements determines the result.
:::{.note}
The `<` operator can also be used to compare lists using a boolean condition. (e.g. `[1 2] < [1 3]` is `true`).
See also [language operators](https://nix.dev/manual/nix/stable/language/operators#comparison) for more information.
:::
# Inputs # Inputs
`cmp` `cmp`
: 1\. Function argument : The comparison function `a: b: ...` must return:
- `0` if `a` and `b` are equal
- `1` if `a` is greater than `b`
- `-1` if `a` is less than `b`
See [lib.compare](#function-library-lib.trivial.compare) for a an example implementation.
`a` `a`
: 2\. Function argument : The first list
`b` `b`
: 3\. Function argument : The second list
# Examples # Examples
:::{.example} :::{.example}
## `lib.lists.compareLists` usage example ## `lib.lists.compareLists` usage examples
```nix ```nix
compareLists compare [] [] compareLists lib.compare [] []
=> 0 => 0
compareLists compare [] [ "a" ] compareLists lib.compare [] [ "a" ]
=> -1 => -1
compareLists compare [ "a" ] [] compareLists lib.compare [ "a" ] []
=> 1 => 1
compareLists compare [ "a" "b" ] [ "a" "c" ] compareLists lib.compare [ "a" "b" ] [ "a" "c" ]
=> -1 => -1
``` ```