[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
lexicographic list comparison
From: |
Sam Steingold |
Subject: |
lexicographic list comparison |
Date: |
Fri, 09 Sep 2022 15:27:46 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (darwin) |
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.
- lexicographic list comparison,
Sam Steingold <=