Function: compat--sort

compat--sort is a byte-compiled function defined in compat-30.el.

Signature

(compat--sort SEQ &optional LESSP &rest REST)

Documentation

[Compatibility function for sort, defined in Emacs 30.1. See (compat) Emacs
30.1' for more details.]

Sort function with support for keyword arguments. The following arguments are defined:

:key FUNC -- FUNC is a function that takes a single element from SEQ and returns
  the key value to be used in comparison. If absent or nil, identity is used.

:lessp FUNC -- FUNC is a function that takes two arguments and returns non-nil
  if the first element should come before the second. If absent or nil,
  value< is used.

:reverse BOOL -- if BOOL is non-nil, the sorting order implied by FUNC is
  reversed. This does not affect stability: equal elements still retain their
  order in the input sequence.

:in-place BOOL -- if BOOL is non-nil, SEQ is sorted in-place and returned.
  Otherwise, a sorted copy of SEQ is returned and SEQ remains unmodified; this
  is the default.

For compatibility, the calling convention (sort SEQ LESSP) can also be used; in this case, sorting is always done in-place.

Source Code

;; Defined in ~/.emacs.d/elpa/compat-30.1.0.1/compat-30.el
(compat-defun sort (seq &optional lessp &rest rest) ;; <compat-tests:sort>
  "Sort function with support for keyword arguments.
The following arguments are defined:

:key FUNC -- FUNC is a function that takes a single element from SEQ and
  returns the key value to be used in comparison.  If absent or nil,
  `identity' is used.

:lessp FUNC -- FUNC is a function that takes two arguments and returns
  non-nil if the first element should come before the second.
  If absent or nil, `value<' is used.

:reverse BOOL -- if BOOL is non-nil, the sorting order implied by FUNC is
  reversed.  This does not affect stability: equal elements still retain
  their order in the input sequence.

:in-place BOOL -- if BOOL is non-nil, SEQ is sorted in-place and returned.
  Otherwise, a sorted copy of SEQ is returned and SEQ remains unmodified;
  this is the default.

For compatibility, the calling convention (sort SEQ LESSP) can also be used;
in this case, sorting is always done in-place."
  :extended t
  (let ((in-place t) (reverse nil) (orig-seq seq))
    (when (or (not lessp) rest)
      (setq
       rest (if lessp (cons lessp rest) rest)
       in-place (plist-get rest :in-place)
       reverse (plist-get rest :reverse)
       lessp (let ((key (plist-get rest :key))
                   (< (or (plist-get rest :lessp) #'value<)))
               (if key
                 (lambda (a b) (funcall < (funcall key a) (funcall key b)))
                 <))
       seq (if (or (and (eval-when-compile (< emacs-major-version 25)) (vectorp orig-seq))
                   in-place)
               seq
             (copy-sequence seq))))
    ;; Emacs 24 does not support vectors. Convert to list.
    (when (and (eval-when-compile (< emacs-major-version 25)) (vectorp orig-seq))
      (setq seq (append seq nil)))
    (setq seq (if reverse
                  (nreverse (sort (nreverse seq) lessp))
                (sort seq lessp)))
    ;; Emacs 24: Convert back to vector.
    (if (and (eval-when-compile (< emacs-major-version 25)) (vectorp orig-seq))
        (if in-place
            (cl-loop for i from 0 for x in seq
                     do (aset orig-seq i x)
                     finally return orig-seq)
          (apply #'vector seq))
      seq)))