Sort list of dictionaries

Jason Friedman jsf80238 at gmail.com
Tue Mar 3 00:33:35 EST 2015


>> 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.

Also, Python 3.4 comes with an ipaddress module.

>>> import ipaddress
>>> address_string_1 = "2.2.3.4"
>>> address_string_2 = "10.2.3.4"
>>> address_string_2 > address_string_1
False
>>> ip_address_1 = ipaddress.ip_address(address_string_1)
>>> ip_address_2 = ipaddress.ip_address(address_string_2)
>>> ip_address_2 > ip_address_1
True



More information about the Python-list mailing list