[Top][All Lists]

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

Re: sorting backwards?

From: David Kastrup
Subject: Re: sorting backwards?
Date: Sun, 15 May 2005 12:17:32 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

Joe Corneli <address@hidden> writes:

> I just posted some code on gnu-emacs-sources that includes some
> sorting.  I want to get numbers in decreasing order, but I found that
> substituting a ">" for a "<" when sorting screws things up.  (Lots of
> contents are omitted.)

No, it isn't.

> Since both < and > are symmetrical mathematically, I don't see why
> this would be.

It isn't.

> I don't have a great example to hand you right now, but if you grab
> the fr3q.el code I just posted and substitute, 
>   (let (1graphs)
>     (maphash (lambda (key val) 
>                (setq 1graphs (cons (cons key val) 1graphs)))
>              fr3q-1graphs)
>     (sort 1graphs (lambda (a b) (> (cdr a) (cdr b)))) ...
> for
>   (let (1graphs)
>     (maphash (lambda (key val) 
>                (setq 1graphs (cons (cons key val) 1graphs)))
>              fr3q-1graphs)
>     (sort 1graphs (lambda (a b) (< (cdr a) (cdr b)))) ...

Both are equally wrong.

C-h f sort RET

    sort is a built-in function in `C source code'.

    Sort LIST, stably, comparing elements using PREDICATE.
    Returns the sorted list.  LIST is modified by side effects.
    PREDICATE is called with two elements of LIST, and should return t
    if the first element is "less" than the second.


You throw away the sorted list!  Instead you rely on the side effects
only.  But this can only work in case that the the sorting procedure
does not create _any_ new conses, and that the sorting does _NOT_
change the position of the first element of the list, since this will
_necessarily_ stay the first element of the "sorted list".

In short, it is merely an accident if your code works, and quite

David Kastrup, Kriemhildstr. 15, 44793 Bochum

reply via email to

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