Sort list of dictionaries

Ian Kelly ian.g.kelly at gmail.com
Mon Mar 2 13:55:27 EST 2015


On Mon, Mar 2, 2015 at 11:38 AM, Charles Heizer <ceh329 at gmail.com> wrote:
> Sorry,
>
> sortedlist = sorted(mylist , key=lambda elem: "%s %s" % (elem['name'], LooseVersion(elem['version'])), reverse=True)
>
> This is what I was trying but LooseVersion() was not sorting version numbers like I thought it would. You will notice that Chrome version "40.0.2214.111" is higher than "40.0.2214.91" but in the end result it's not sorting it that way.

Because it's a string they're sorted lexicographically, and in that
ordering "40.0.2214.111" is less than "40.0.2214.91". Instead of a
string you should probably use some sort of version info tuple. A
simple tuple of ints may suffice, although you may need to get a
little cleverer if there are ever any version strings that aren't
entirely dotted numeric.



More information about the Python-list mailing list