unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: David Hansen <david.hansen@gmx.net>
To: help-gnu-emacs@gnu.org
Subject: Re: sorting a list
Date: Thu, 17 Apr 2008 08:34:36 +0200	[thread overview]
Message-ID: <87fxtlgfb7.fsf@localhorst.mine.nu> (raw)
In-Reply-To: loom.20080417T061701-911@post.gmane.org

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"))






  reply	other threads:[~2008-04-17  6:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-17  6:18 sorting a list Seweryn Kokot
2008-04-17  6:34 ` David Hansen [this message]
     [not found] <mailman.10469.1208413210.18990.help-gnu-emacs@gnu.org>
2008-04-17 18:09 ` Pascal Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fxtlgfb7.fsf@localhorst.mine.nu \
    --to=david.hansen@gmx.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).