summaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorMattias EngdegÄrd <mattiase@acm.org>2024-03-19 13:03:47 +0100
committerMattias EngdegÄrd <mattiase@acm.org>2024-03-29 11:39:38 +0100
commitae5f2c02bd2fc269e2cc32c8039d95fbf4225e69 (patch)
treea4c4b2d9cb7288524b7946e0f3263dca4357fd9c /etc
parenta52f1121a3589af8f89828e04d66f1215c361bcf (diff)
downloademacs-ae5f2c02bd2fc269e2cc32c8039d95fbf4225e69.tar.gz
New `sort` keyword arguments (bug#69709)
Add the :key, :lessp, :reverse and :in-place keyword arguments. The old calling style remains available and is unchanged. * src/fns.c (sort_list, sort_vector, Fsort): * src/sort.c (tim_sort): Add keyword arguments with associated new features. All callers of Fsort adapted. * test/src/fns-tests.el (fns-tests--shuffle-vector, fns-tests-sort-kw): New test. * doc/lispref/sequences.texi (Sequence Functions): Update manual. * etc/NEWS: Announce.
Diffstat (limited to 'etc')
-rw-r--r--etc/NEWS25
1 files changed, 25 insertions, 0 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 73ffff9f2d3..4018df1fecb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1770,6 +1770,31 @@ lexicographically.
It is intended as a convenient ordering predicate for sorting, and is
likely to be faster than hand-written Lisp functions.
++++
+** New 'sort' arguments and features.
+The 'sort' function can now be called using the signature
+
+ (sort SEQ &rest KEYWORD-ARGUMENTS)
+
+where arguments after the first are keyword/value pairs, all optional:
+':key' specifies a function that produces the sorting key from an element,
+':lessp' specifies the ordering predicate, defaulting to 'value<',
+':reverse' is used to reverse the sorting order,
+':in-place is used for in-place sorting, as the default is now to
+sort a copy of the input.
+
+The new signature is less error-prone and reduces the need to write
+ordering predicates by hand. We recommend that you use the ':key'
+argument instead of ':lessp' unless a suitable ordering predicate is
+already available. This can also be used for multi-key sorting:
+
+ (sort seq :key (lambda (x) (list (age x) (size x) (cost x))))
+
+sorts by the return value of 'age', then by 'size', then by 'cost'.
+
+The old signature, '(sort SEQ PREDICATE)', can still be used and sorts
+its input in-place as before.
+
** New function 'sort-on'.
This function implements the Schwartzian transform, and is appropriate
for sorting lists when the computation of the sort key of a list