Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string': 0.09; '%s"': 0.09; 'charles': 0.16; 'dotted': 0.16; 'elem:': 0.16; 'sorting': 0.16; 'subject:Sort': 0.16; 'subject:dictionaries': 0.16; 'tuple': 0.16; 'tuple.': 0.16; 'wrote:': 0.18; 'trying': 0.19; "aren't": 0.24; 'skip:l 30': 0.24; 'sorry,': 0.24; 'mon,': 0.24; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'probably': 0.32; 'entirely': 0.33; 'info': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'numbers': 0.61; 'simple': 0.61; 'mar': 0.68; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=6Lo4mqN9PkjWJw2g7CeH8NEORNwSZAtRa3ziqKDdHnA=; b=m2lwhAf9x8+j7EwGagO+d0xlnMl7METH3b4K9ldKBWkyupRowpOlGK98tseLaVMfHr rzecFXokgYb8lK+AuOXCCHDufk3XW3qVSLsuvyHL8N+RgfwjjDGpLAJwRU2+adPbLclC LB3Tlkjk63p5FJMYGekuiBmIkGTwWqwVW7z+V1nVZojTbFZVIBbo82iNnveC4pyWcMYT 0cQphHNFihoJKFWVLk+YnSCzYpTtQ37Ptv0+voM+KjUkOsjRhs/ahYiDUJ1tXZq3mye9 3rPryIHQzkuDebC4efV+yvj1sT/vATbN3iA91cydC4Q43XkaN4pCQi6Np/2FW9Vk/zZR zQ/A== X-Received: by 10.194.236.133 with SMTP id uu5mr340822wjc.155.1425322568735; Mon, 02 Mar 2015 10:56:08 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> References: <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> From: Ian Kelly Date: Mon, 2 Mar 2015 11:55:27 -0700 Subject: Re: Sort list of dictionaries To: 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: 13 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425322577 news.xs4all.nl 2882 [2001:888:2000:d::a6]:39470 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86771 On Mon, Mar 2, 2015 at 11:38 AM, Charles Heizer 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.