* lisp/emacs-lisp/seq.el (seq-sort-by): New function.
* test/lisp/emacs-lisp/seq-tests.el: New test for seq-sort-by.
* doc/lispref/sequences.texi: Add documentation for seq-sort-by.
non-@code{nil} if the first argument should sort before the second.
@end defun
+@defun seq-sort-by function predicate sequence
+ This function is similar to @code{seq-sort}, but the elements of
+@var{sequence} are transformed by applying @var{function} on them
+before being sorted. @var{function} is a function of one argument.
+
+@example
+(seq-sort-by #'seq-length #'> ["a" "ab" "abc"])
+@result{} ["abc" "ab" "a"]
+@end example
+@end defun
+
+
@defun seq-contains sequence elt &optional function
This function returns the first element in @var{sequence} that is equal to
@var{elt}. If the optional argument @var{function} is non-@code{nil},
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: sequences
-;; Version: 2.3
+;; Version: 2.14
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
(cl-defmethod seq-sort (pred (list list))
(sort (seq-copy list) pred))
+(defun seq-sort-by (function pred sequence)
+ "Sort SEQUENCE using PRED as a comparison function.
+Elements of SEQUENCE are transformed by FUNCTION before being
+sorted. FUNCTION must be a function of one argument."
+ (seq-sort (lambda (a b)
+ (funcall pred
+ (funcall function a)
+ (funcall function b)))
+ sequence))
+
(cl-defgeneric seq-reverse (sequence)
"Return a sequence with elements of SEQUENCE in reverse order."
(let ((result '()))
(should (= (seq-position seq 'a #'eq) 0))
(should (null (seq-position seq (make-symbol "a") #'eq)))))
+(ert-deftest test-seq-sort-by ()
+ (let ((seq ["x" "xx" "xxx"]))
+ (should (equal (seq-sort-by #'seq-length #'> seq)
+ ["xxx" "xx" "x"]))))
+
(provide 'seq-tests)
;;; seq-tests.el ends here