aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJรถrg Thalheim <Mic92@users.noreply.github.com>2018-04-08 22:44:15 +0100
committerGitHub <noreply@github.com>2018-04-08 22:44:15 +0100
commit0fe11dbeddb06e319483e16770646ba144a3e833 (patch)
treecef054de430ea2130dd14c7d7a7d70b45e946b0a /lib
parent9f02802caa445f4c41eed8dfa42c2acd35381d14 (diff)
parent3f8c9106ea7cbe32cb9a95c6e952dca90387a0ca (diff)
Merge pull request #38611 from volth/nat-sort
lib: add naturalSort
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix2
-rw-r--r--lib/lists.nix23
2 files changed, 23 insertions, 2 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 59b3d2159da..c292ed33e1d 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -74,7 +74,7 @@ let
inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
concatMap flatten remove findSingle findFirst any all count
optional optionals toList range partition zipListsWith zipLists
- reverseList listDfs toposort sort compareLists take drop sublist
+ reverseList listDfs toposort sort naturalSort compareLists take drop sublist
last init crossLists unique intersectLists subtractLists
mutuallyExclusive;
inherit (strings) concatStrings concatMapStrings concatImapStrings
diff --git a/lib/lists.nix b/lib/lists.nix
index 424d2c57f55..5ec97f5a07f 100644
--- a/lib/lists.nix
+++ b/lib/lists.nix
@@ -1,7 +1,9 @@
# General list operations.
{ lib }:
with lib.trivial;
-
+let
+ inherit (lib.strings) toInt;
+in
rec {
inherit (builtins) head tail length isList elemAt concatLists filter elem genList;
@@ -409,6 +411,25 @@ rec {
then compareLists cmp (tail a) (tail b)
else rel;
+ /* Sort list using "Natural sorting".
+ Numeric portions of strings are sorted in numeric order.
+
+ Example:
+ naturalSort ["disk11" "disk8" "disk100" "disk9"]
+ => ["disk8" "disk9" "disk11" "disk100"]
+ naturalSort ["10.46.133.149" "10.5.16.62" "10.54.16.25"]
+ => ["10.5.16.62" "10.46.133.149" "10.54.16.25"]
+ naturalSort ["v0.2" "v0.15" "v0.0.9"]
+ => [ "v0.0.9" "v0.2" "v0.15" ]
+ */
+ naturalSort = lst:
+ let
+ vectorise = s: map (x: if isList x then toInt (head x) else x) (builtins.split "(0|[1-9][0-9]*)" s);
+ prepared = map (x: [ (vectorise x) x ]) lst; # remember vectorised version for O(n) regex splits
+ less = a: b: (compareLists compare (head a) (head b)) < 0;
+ in
+ map (x: elemAt x 1) (sort less prepared);
+
/* Return the first (at most) N elements of a list.
Example: