all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* lexicographic list comparison
@ 2022-09-09 19:27 Sam Steingold
  2022-09-10  9:07 ` Mattias Engdegård
  0 siblings, 1 reply; 10+ messages in thread
From: Sam Steingold @ 2022-09-09 19:27 UTC (permalink / raw)
  To: emacs-devel

Hi,

What do you do when sorting a list of lists of numbers?

There does not appear to be a standard comparison function like
--8<---------------cut here---------------start------------->8---
(defun lexicographic-compare-lists (l1 l2 &optional lessp)
  "lexicographic list comparison"
  (unless lessp
    (setq lessp #'<))
  (or (null l2) (null l1)
      (funcall lessp (car l1) (car l2))
      (and (not (funcall lessp (car l2) (car l1)))
           (lexicographic-compare-lists (cdr l1) (cdr l2) lessp))))
--8<---------------cut here---------------end--------------->8---

[[[It appears that it is less critical for strings because one can do
--8<---------------cut here---------------start------------->8---
(sort list-of-lists-of-strings
      (lambda (l1 l2) (string< (string-join l1) (string-join l2))))
--8<---------------cut here---------------end--------------->8---
instead of
--8<---------------cut here---------------start------------->8---
(sort list-of-lists-of-strings
      (lambda (l1 l2) (lexicographic-compare-lists l1 l2 #'string<)))
--8<---------------cut here---------------end--------------->8---
even though it conses for each comparison(!)]]]

Or maybe sorting lists of lists is just such a rare op that no one has
ever encountered it before me? ;-)

Thank you.

-- 
Sam Steingold (https://aphar.dreamwidth.org/) on darwin Ns 10.3.2113
https://lastingimpactpsychology.com https://steingoldpsychology.com
https://ij.org/ https://honestreporting.com https://memri.org
You do not have time or money to sue anyone rich enough to be worth suing.



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-09-12 19:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-09 19:27 lexicographic list comparison Sam Steingold
2022-09-10  9:07 ` Mattias Engdegård
2022-09-10  9:22   ` Emanuel Berg
2022-09-10  9:26   ` tomas
2022-09-12 14:45   ` Sam Steingold
2022-09-12 17:53     ` mattiase
2022-09-12 18:31       ` Stefan Monnier
2022-09-12 18:41         ` Sam Steingold
2022-09-12 19:32           ` Stefan Monnier
2022-09-12 18:35       ` Sam Steingold

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.