Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86822

Re: Sort list of dictionaries

References <f1a339d0-0386-43ba-b6b4-1aee39d75581@googlegroups.com> <mailman.56.1425321135.13471.python-list@python.org> <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> <CALwzidmMXXoumCW2rVtY_Yh5Dp_hJ_4Tcrp736OYvkp6TTyFAA@mail.gmail.com> <CANy1k1gFfW5m_nJrmXqVGU_sYHD9tFHP3Dshc_H=oaJru-6cxg@mail.gmail.com>
Date 2015-03-03 18:07 +1100
Subject Re: Sort list of dictionaries
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4.1425366433.21433.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Mar 3, 2015 at 4:33 PM, Jason Friedman <jsf80238@gmail.com> wrote:
>>> 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.

Heh, I think that miiiiiiiight be a bit abusive :) I'm not sure that
you want to depend on the version numbers fitting inside the rules for
IP addresses, especially given that the example has a component of
"2214".

The right way to compare version numbers is usually to split them on
dots, then treat each part as a separate number. I say "usually"
because there are complications like "alpha", "RC", "-git", "+deb7u1",
and so on, but if the entire version number consists of digits and
dots, then tuple(int(x) for x in version_number.split(".")) will give
you a properly-sortable key.

BTW, Jason: It's usually courteous to acknowledge who you're quoting.
If you look at the top of my post here, you'll see that there's a line
saying your name and email address, and the date/time that you made
your post; but underneath that is just straight text, because your
post didn't extend the same courtesy to the previous posters. When you
trim quoted text, do please try to keep at least the first
acknowledgement line - the one showing who you're actually quoting.
Whether or not you keep additional headers is up to you (sometimes
it's easy, but other times it's fiddly), but the first one is your
responsibility. Thanks!

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:17 -0800
  Re: Sort list of dictionaries Emile van Sebille <emile@fenx.com> - 2015-03-02 10:31 -0800
    Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:38 -0800
      Re: Sort list of dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-02 11:55 -0700
      Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:58 -0800
        Re: Sort list of dictionaries Peter Otten <__peter__@web.de> - 2015-03-02 20:23 +0100
          Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-03 07:56 -0800
            Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-04 03:09 +1100
              Re: Sort list of dictionaries Paul  Moore <p.f.moore@gmail.com> - 2015-03-03 08:48 -0800
            Re: Sort list of dictionaries Peter Otten <__peter__@web.de> - 2015-03-03 18:44 +0100
      Re: Sort list of dictionaries Dave Angel <davea@davea.name> - 2015-03-02 13:59 -0500
      Re: Sort list of dictionaries Jason Friedman <jsf80238@gmail.com> - 2015-03-02 22:33 -0700
      Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-03 18:07 +1100
      Re: Sort list of dictionaries Jason Friedman <jsf80238@gmail.com> - 2015-03-03 07:45 -0700
      Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-04 01:50 +1100
  Re: Sort list of dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 18:55 +1100

csiph-web