about summary refs log tree commit diff
path: root/lib/tests/misc.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-12-07 19:51:07 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-12-08 22:15:29 +0100
commit67cc78643d7307b7a4cd783e43c53346fdea18bf (patch)
tree123b32ba0139e585d1fbbf98a9ba4f42e3f69893 /lib/tests/misc.nix
parentb3caa5f50a90a069b1f9596b48be6cb95d94171b (diff)
lib.sortOn: init
A more efficient sort in some cases, and often convenient.

This exposes `lib.lists.sortOn` immediately on `lib`, because it is
a sibling of `sort`, which is already present there.
Omitting it would lead to more confusion, and worse outcomes.
There's no confusion about the types `sort` or `sortOn` operate on.

Haskell agrees about the type for `sortOn`, and it is in its `base`.
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r--lib/tests/misc.nix22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 9f1fee2ba2341..608af656d02c0 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -650,6 +650,28 @@ runTests {
     expected = [2 30 40 42];
   };
 
+  testSortOn = {
+    expr = sortOn stringLength [ "aa" "b" "cccc" ];
+    expected = [ "b" "aa" "cccc" ];
+  };
+
+  testSortOnEmpty = {
+    expr = sortOn (throw "nope") [ ];
+    expected = [ ];
+  };
+
+  testSortOnIncomparable = {
+    expr =
+      map
+        (x: x.f x.ok)
+        (sortOn (x: x.ok) [
+          { ok = 1; f = x: x; }
+          { ok = 3; f = x: x + 3; }
+          { ok = 2; f = x: x; }
+        ]);
+    expected = [ 1 2 6 ];
+  };
+
   testReplicate = {
     expr = replicate 3 "a";
     expected = ["a" "a" "a"];