emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Weird sort/string-lessp behavior


From: Teemu Likonen
Subject: Re: Weird sort/string-lessp behavior
Date: Fri, 14 Aug 2009 17:31:52 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

On 2009-08-14 16:01 (+0200), Deniz Dogan wrote:
> Can this ever happen?
>
> (message "Now %d length" (length attributes))
> (sort attributes #'string-lessp)
> (message "Now %d length" (length attributes))
>
> Output:
> Now 3 length
> Now 2 length

Yes, let us quote info page "(elisp) Rearrangement":

    The destructive aspect of `sort' is that it rearranges the cons
    cells forming LIST by changing CDRs. A nondestructive sort function
    would create new cons cells to store the elements in their sorted
    order. If you wish to make a sorted copy without destroying the
    original, copy it first with `copy-sequence' and then sort.

    Sorting does not change the CARs of the cons cells in LIST; the cons
    cell that originally contained the element `a' in LIST still has `a'
    in its CAR after sorting, but it now appears in a different position
    in the list due to the change of CDRs. For example:

         (setq nums '(1 3 2 6 5 4 0))
              => (1 3 2 6 5 4 0)
         (sort nums '<)
              => (0 1 2 3 4 5 6)
         nums
              => (1 2 3 4 5 6)

    *Warning*: Note that the list in `nums' no longer contains 0; this
    is the same cons cell that it was before, but it is no longer the
    first one in the list. Don't assume a variable that formerly held
    the argument now holds the entire sorted list! Instead, save the
    result of `sort' and use that. Most often we store the result back
    into the variable that held the original list:

         (setq nums (sort nums '<))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]