Doubt regarding sorting functions

Brian van den Broek bvande at po-box.mcgill.ca
Mon Apr 11 02:02:31 EDT 2005


praba kar said unto the world upon 2005-04-11 00:50:
> Dear All,
> 
>           I am new to Python.  I am in need of
> some sorting functions (eg) numerical sorting
> functions and alphapetical sorting functions. 
> I have searched through net But I cannot
> find any regarding this so If anyone know
> regarding this. Kindly mail me as early as possible
> 
> with regards
> PRabahr

Hi PRabahr,

 >>> some_references = [
   "<http://docs.python.org/lib/built-in-funcs.html> (look for 'cmp')",
   "<http://docs.python.org/lib/typesseq-mutable.html> (read fn 8)",
   "<http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Searching>",
   "<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234> ]
 >>> some_references.sort()
 >>> for r in some_references:
         print r

<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234>
<http://aspn.activestate.com/ASPN/Cookbook/Python?kwd=Searching>
<http://docs.python.org/lib/built-in-funcs.html> (look for 'cmp')
<http://docs.python.org/lib/typesseq-mutable.html> (read fn 8)>>>
 >>>

If you want to implement a specific sorting routine, the two dominant 
ways are to either use the decorate-sort-undecorate idiom or define a 
custom cmp function. Both are discussed in the links above.

Best wishes,

Brian vdB



More information about the Python-list mailing list