unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* sorting a list
@ 2008-04-17  6:18 Seweryn Kokot
  2008-04-17  6:34 ` David Hansen
  0 siblings, 1 reply; 3+ messages in thread
From: Seweryn Kokot @ 2008-04-17  6:18 UTC (permalink / raw)
  To: help-gnu-emacs

Assume I have a list

'(("abc" "xxsx") 
  ("zdfa" "xxsx") 
  ("dddbc" "xxsx") 
  ("cabc" "xxsx"))

How to sort the list according to the first element in the lists so to
get

'(("abc" "xxsx")
  ("cabc" "xxsx")
  ("dddbc" "xxsx")
  ("zdfa" "xxsx"))

Thanks in advance.






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

* Re: sorting a list
  2008-04-17  6:18 Seweryn Kokot
@ 2008-04-17  6:34 ` David Hansen
  0 siblings, 0 replies; 3+ messages in thread
From: David Hansen @ 2008-04-17  6:34 UTC (permalink / raw)
  To: help-gnu-emacs

On Thu, 17 Apr 2008 06:18:39 +0000 (UTC) Seweryn Kokot wrote:

> Assume I have a list
>
> '(("abc" "xxsx") 
>   ("zdfa" "xxsx") 
>   ("dddbc" "xxsx") 
>   ("cabc" "xxsx"))
>
> How to sort the list according to the first element in the lists so to
> get
>
> '(("abc" "xxsx")
>   ("cabc" "xxsx")
>   ("dddbc" "xxsx")
>   ("zdfa" "xxsx"))
>

(sort '(("abc" "xxsx") 
        ("zdfa" "xxsx") 
        ("dddbc" "xxsx") 
        ("cabc" "xxsx"))
      #'(lambda (list1 list2)
          (string< (car list1) (car list2))))

(("abc" "xxsx") ("cabc" "xxsx") ("dddbc" "xxsx") ("zdfa" "xxsx"))

or if you like the cl equivalent more:

(sort* '(("abc" "xxsx") 
        ("zdfa" "xxsx") 
        ("dddbc" "xxsx") 
        ("cabc" "xxsx"))
       #'string<
       :key #'car)

(("abc" "xxsx") ("cabc" "xxsx") ("dddbc" "xxsx") ("zdfa" "xxsx"))






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

* Re: sorting a list
       [not found] <mailman.10469.1208413210.18990.help-gnu-emacs@gnu.org>
@ 2008-04-17 18:09 ` Pascal Bourguignon
  0 siblings, 0 replies; 3+ messages in thread
From: Pascal Bourguignon @ 2008-04-17 18:09 UTC (permalink / raw)
  To: help-gnu-emacs

Seweryn Kokot <skokot@o2.pl> writes:

> Assume I have a list
>
> '(("abc" "xxsx") 
>   ("zdfa" "xxsx") 
>   ("dddbc" "xxsx") 
>   ("cabc" "xxsx"))
>
> How to sort the list according to the first element in the lists so to
> get
>
> '(("abc" "xxsx")
>   ("cabc" "xxsx")
>   ("dddbc" "xxsx")
>   ("zdfa" "xxsx"))

(require 'cl)
(sort* list (function string<) :key (function first))

Note that sort and sort* are destructive of the original list.  
You must not give it a literal list.

(defun f-good ()
 (let ((list '(("abc" "xxsx") 
               ("zdfa" "xxsx") 
               ("dddbc" "xxsx") 
               ("cabc" "xxsx"))))
  (list (second list)
        (first (sort* (copy-list list)
                      (function string<) :key (function first))))))

(defun f-bad ()
 (let ((list '(("abc" "xxsx") 
               ("zdfa" "xxsx") 
               ("dddbc" "xxsx") 
               ("cabc" "xxsx"))))
  (list (second list)
        (first (sort* list
                      (function string<) :key (function first))))))

(list (f-good) (f-good))
--> ((#1=("zdfa" "xxsx") #2=("abc" "xxsx")) (#1# #2#))

(list (f-bad) (f-bad))
--> ((("zdfa" "xxsx") #1=("abc" "xxsx")) (("cabc" "xxsx") #1#))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

READ THIS BEFORE OPENING PACKAGE: According to certain suggested
versions of the Grand Unified Theory, the primary particles
constituting this product may decay to nothingness within the next
four hundred million years.


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

end of thread, other threads:[~2008-04-17 18:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.10469.1208413210.18990.help-gnu-emacs@gnu.org>
2008-04-17 18:09 ` sorting a list Pascal Bourguignon
2008-04-17  6:18 Seweryn Kokot
2008-04-17  6:34 ` David Hansen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).