From a7e4524470d9ff37288d111a0b118e56d0fee069 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sat, 17 Oct 2020 13:32:57 +0700 Subject: [PATCH] add usort --- README.md | 1 + src/Lib/Collections.php | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 0e6d2a4..b071bee 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ keys | `(keys {"a" 1 "b" 2})` | `("a" "b")` | Return a list of the keys for a values | `(values {"a" 1 "b" 2})` | `(1 2)` | Return a list of the values for a hash-map. zip | `(zip ["a" "b"] [1 2])` | `{"a":1 "b":2}` | Create a hash-map using the first sequence as keys and the second as values. Uses [array_combine](https://www.php.net/manual/en/function.array-combine.php) internally. sort | `(sort [6 4 8 1])` | `[1 4 6 8]` | Sort the sequence using [sort](https://www.php.net/manual/en/function.sort.php). +usort | `(usort (fn (a b) (if (< a b) 0 1)) [3 1 5 4 2])` | `[1 2 3 4 5]` | Sort the sequence using custom comparison function using [usort](https://www.php.net/manual/en/function.usort.php). ### Comparison functions diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index 48548b1..aa7632e 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -240,5 +240,13 @@ class Collections implements ILib return $a::new($data); } )); + + $env->set('usort', new CoreFunc('usort', 'Sort the sequence using custom comparison function.', 2, 2, + function (Func $f, Seq $a) { + $data = $a->getData(); + usort($data, $f->getClosure()); + return $a::new($data); + } + )); } }