[Tutor] Logical Sorting

Python python at venix.com
Fri Jul 7 21:44:25 CEST 2006


On Fri, 2006-07-07 at 10:09 -0700, Evan Klitzke wrote:
> Hi,
> 
> I am trying to sort a list of directories that correspond to kernel
> sources under /usr/src/linux.  I wrote some code that gets a list like
> this:
> ['linux-2.6.9-gentoo-r4', 'linux-2.6.16-gentoo-r11/', 'linux-2.6.16-gentoo-r7/']
> 
> When I sort the list, I want it to go from oldest (lowest version) to
> newest, so the sorted list should look like this:
> ['linux-2.6.9-gentoo-r4', 'linux-2.6.16-gentoo-r7/', 'linux-2.6.16-gentoo-r11/']
> 
> The problem is that since the built in string comparisons compare
> character by character, so the sort puts 2.6.16 before 2.6.9, and -r11
> before -r7.  This is obviously not what I want.  My question is: are
> there any modules or built in methods that will do a logical sort on a
> list like this, and sort it the way I want, or will I have to write my
> own sorting function?

There is an rpm-python package that probably includes much of what you
want.  I have it installed, but never used it.  It appears to be pretty
light on documentation.  I expect it is a python wrapper to a C
library.  

You may find it easier to simply pick apart your data and sort the
numbers numerically than wrestle with learning this package unless you
have other RPM related tasks.

> 
> -- Evan Klitzke
> 
> P.S. I know that the simplest way is just to use ls to sort by time,
> but it is not necessarily true that older kernel versions have an
> older time stamp :-)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list