How to sort a list of version strings?

This is the place for queries that don't fit in any of the other categories.

How to sort a list of version strings?

Postby pnelsonsr » Mon Dec 23, 2013 12:16 am

I have a List of strings with numbers in them like:
Code: Select all
filelist=['afile.1.9.8.tar.gz', 'afile.1.9.4.tar.gz', 'afile.1.10.1.tar.gz']

I've tried to sort the list with
Code: Select all
filelist.sort()

But this produces
Code: Select all
['afile.1.10.1.tar.gz','afile.1.9.4.tar.gz', 'afile.1.9.8.tar.gz']

But what I need it to sort to is
Code: Select all
['afile.1.9.4.tar.gz', 'afile.1.9.8.tar.gz','afile.1.10.1.tar.gz']

Anyone have a good way sorting to this outcome?
Last edited by Yoriz on Mon Dec 23, 2013 10:26 am, edited 1 time in total.
Reason: changed title
pnelsonsr
 
Posts: 25
Joined: Thu Dec 12, 2013 12:48 am

Re: String with numbers sorting

Postby Yoriz » Mon Dec 23, 2013 8:31 am

Code: Select all
from distutils import version

filelist = ['afile.1.9.8.tar.gz', 'afile.1.9.4.tar.gz', 'afile.1.10.1.tar.gz']
filelist.sort(key=version.LooseVersion)

Produces
Code: Select all
['afile.1.9.4.tar.gz', 'afile.1.9.8.tar.gz', 'afile.1.10.1.tar.gz']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: How to sort a list of version strings?

Postby micseydel » Mon Dec 23, 2013 7:00 pm

+1 Yoriz!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: String with numbers sorting

Postby pnelsonsr » Mon Dec 23, 2013 8:49 pm

Yoriz wrote:
Code: Select all
from distutils import version

filelist = ['afile.1.9.8.tar.gz', 'afile.1.9.4.tar.gz', 'afile.1.10.1.tar.gz']
filelist.sort(key=version.LooseVersion)

Produces
Code: Select all
['afile.1.9.4.tar.gz', 'afile.1.9.8.tar.gz', 'afile.1.10.1.tar.gz']


OM Gosh... That is perfect. I had spent hours working with re to try and get this working but was unsuccessful. Thanks Yoriz! This is just what I needed.
pnelsonsr
 
Posts: 25
Joined: Thu Dec 12, 2013 12:48 am

Re: String with numbers sorting

Postby Kebap » Mon Dec 23, 2013 9:37 pm

pnelsonsr wrote:I had spent hours working with re to try and get this working but was unsuccessful.


I would not use re, instead split the string at the dots. Then sort the numbers one by one. You can pass you own comparisons to be executed for sorting.

Code: Select all
>>> ver = 'afile.1.9.8.tar.gz'
>>> ver.split(".")
['afile', '1', '9', '8', 'tar', 'gz']
>>>

I don't really know by heart how to do the last part though, so may have taken me some minutes or hours to get that straight, too. :D Advantage over built-in functions (always good to know those): You can flexibly expand to your very needs, when they derive from standard.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 394
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: String with numbers sorting

Postby pnelsonsr » Tue Dec 24, 2013 2:13 am

Kebap wrote:I would not use re, instead split the string at the dots. Then sort the numbers one by one. You can pass you own comparisons to be executed for sorting.

I don't really know by heart how to do the last part though, so may have taken me some minutes or hours to get that straight, too. :D Advantage over built-in functions (always good to know those): You can flexibly expand to your very needs, when they derive from standard.


Split works too. I was doing the re in sort() like Yoriz only with a lambda like:

Code: Select all
import re
filelist.sort(key=lambda file: re.search('^\D+\.(\d+)\.(\d+)\.(\d+)\.tar.gz',file).group(1))


But I couldn't figure out how to do the three sorts together. The "version.LooseVersion" did it perfectly and it follows standards versioning sorting of pythons many other tools (the pre and post version stuff in peticular).
pnelsonsr
 
Posts: 25
Joined: Thu Dec 12, 2013 12:48 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests