Perl-Python-a-Day: Sorting

Pascal Costanza pc at p-cos.net
Mon Oct 10 15:05:09 CEST 2005


Ulrich Hobelmann wrote:
> Xah Lee wrote:
> 
>> To sort a list in Python, use the “sort” method. For example:
>>
>> li=[1,9,2,3];
>> li.sort();
>> print li;
> 
> Likewise in Common Lisp.  In Scheme there are probably packages for that 
> as well.  My apologies for not being very fluent anymore.
> 
> CL-USER> (setf list (sort '(1 9 2 3) #'<))    ; input
> (1 2 3 9)                    ; output

Careful. Common Lisp's sort function is specified to be destructive, so 
you shouldn't use it on literal constants. So don't say (sort '(1 9 2 3) 
...), say (sort (list 1 9 2 3) ...), etc.


Pascal

-- 
OOPSLA'05 tutorial on generic functions & the CLOS Metaobject Protocol
++++ see http://p-cos.net/oopsla05-tutorial.html for more details ++++



More information about the Python-list mailing list