Subject: [clisp-list] sorting a list of sublists, based on the sublists first value

From: "David Brynjar Franzson" <[email-masked]>
Date: Sun Nov 2 14:11:03 2003

Hi, I am trying to sort a list containing sublists ((50 3500) (25 2570) (40
2800)) based on the first value of each sublist.

A normal sort-list function is sth like this...

(defun sort-list (list &optional (pred '<))
    (cond ((null) list) nil )
        ((atom (first list)) (sort list pred))
            (t (cons (sort-list (first list) pred) (sort-list (rest list)
pred)))))

Being almost completely illiterate in Lisp, I am guessing that I just need
to plugin in sth in the order of "(nth 0 sublist)" somewhere in this
function.

Any help on this would be greatly appreciated.

Sincerely,
David B Franzson
[email-masked]

---

From: Russell McManus <[email-masked]>
Date: Sun Nov 2 14:47:17 2003

This can be handled using the :key keyword arg to sort,
like so:

  (sort '((50 3500) (25 2570) (40 2800))
        #'<
        :key #'first)

-russ

"David Brynjar Franzson" <[email-masked]> writes:

> -------------------------------------------------------
> This SF.net email is sponsored by: SF.net Giveback Program.
> Does SourceForge.net help you be more productive?  Does it
> help you create better code?   SHARE THE LOVE, and help us help
> YOU!  Click Here: http://sourceforge.net/donate/
> _______________________________________________
> clisp-list mailing list
> [email-masked]
> https://lists.sourceforge.net/lists/listinfo/clisp-list

---

From: Pascal J.Bourguignon <[email-masked]>
Date: Sun Nov 2 14:48:05 2003

General lisp questions may be posed on news:comp.lang.lisp too.

> 2800)) based on the first value of each sublist.

      (sort list (lambda (a b) (<= (first a) (first b))))

[75]> (setq list '((50 3500) (25 2570) (40 2800)))
((50 3500) (25 2570) (40 2800))
[76]> (sort list (lambda (a b) (<= (first a) (first b))))
((25 2570) (40 2800) (50 3500))
[77]> 

>             (t (cons (sort-list (first list) pred) (sort-list (rest list)

Did I miss  something?  Is it required to sort  the sublists?  What is
it you want exactly?

> to plugin in sth in the order of "(nth 0 sublist)" somewhere in this

(nth 0 sublist)  is effectly the first element of  the sublist, but if
you want the first element of the sublist why not say (first sublist)?

-- 
__Pascal_Bourguignon__
http://www.informatimago.com/

---

From: james anderson <[email-masked]>
Date: Sun Nov 2 15:03:02 2003

(sort (copy-list list) pred :key #'(lambda (entry) (if (consp entry) 
(first entry) entry)))

whereby the copy is necessary iff you wish to continue to use the 
original

On Sunday, Nov 2, 2003, at 22:57 Europe/Berlin, David Brynjar Franzson 

> Any help on this would be greatly appreciated.
...