Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'string': 0.09; 'key.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'dotted': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'quoted': 0.16; 'sorting': 0.16; 'subject:Sort': 0.16; 'subject:dictionaries': 0.16; 'tuple': 0.16; 'tuple.': 0.16; 'component': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'split': 0.19; '>>>': 0.22; 'example': 0.22; 'rules': 0.22; 'saying': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'headers': 0.24; 'text,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'compare': 0.26; 'post': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'on,': 0.29; 'especially': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'usually': 0.31; 'easy,': 0.31; 'probably': 0.32; 'extend': 0.32; 'thanks!': 0.32; 'entirely': 0.33; 'info': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'addresses,': 0.36; 'module.': 0.36; "didn't": 0.36; 'should': 0.36; 'jason': 0.38; 'pm,': 0.38; 'previous': 0.38; 'little': 0.38; 'sure': 0.39; 'consists': 0.60; 'numbers': 0.61; 'entire': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; "you'll": 0.62; 'name': 0.63; 'mar': 0.68; 'address,': 0.75; '2015': 0.84; '3.4': 0.84; 'post;': 0.84; 'underneath': 0.84; 'to:none': 0.92; 'acknowledge': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=HygZEtHHcxKlVmK11tEmnwz6s5qA/diY7u4wvtEZPoo=; b=kaQECWIv870111vbsGblZaTTy4uqxCjHHoUjcHZpNtsgkW6uyd3K1tg3RnfgXnIpKm VVqNPpY8HYRGipUtcQfbLgXOc+MVDRSVm6ihQD4HI09S7Skz3wNntPE3bjHmox5lGzW9 NBeskl12GFcU8eJnzt5jCkBB4AqSYspMZZMMDyIxlpJn2dFVonCKhIqqTUtKCuh8paFQ w/YjUIPqPT2ouAZRYe94oGQvlFFXfjoaFelJjkUmxWfp7WIgXxWZuzRG0tobCD85e4nJ 10Z9PKre491GZlG/joOVEUiJi3lRxIHdkkZ/1XDaDjDQgzZfJm9LPuIZ9+71K3tunK01 H2Kg== MIME-Version: 1.0 X-Received: by 10.50.79.161 with SMTP id k1mr27540086igx.14.1425366424757; Mon, 02 Mar 2015 23:07:04 -0800 (PST) In-Reply-To: References: <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> Date: Tue, 3 Mar 2015 18:07:04 +1100 Subject: Re: Sort list of dictionaries From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425366433 news.xs4all.nl 2901 [2001:888:2000:d::a6]:42213 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86822 On Tue, Mar 3, 2015 at 4:33 PM, Jason Friedman 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