Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'names.': 0.07; 'python': 0.08; 'after,': 0.09; 'callable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'output': 0.10; 'def': 0.13; 'objects)': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:sort': 0.16; 'wrote:': 0.18; 'seems': 0.20; 'subject:list': 0.21; 'from:addr:web.de': 0.23; 'extract': 0.24; 'function': 0.27; 'print': 0.29; 'class': 0.29; 'fairly': 0.30; 'subject:?': 0.31; 'skip:l 30': 0.32; 'list': 0.32; 'objects': 0.32; 'sort': 0.33; 'header:X-Complaints-To:1': 0.33; 'object': 0.33; 'to:addr:python- list': 0.34; 'function.': 0.34; 'skip:k 20': 0.34; 'however,': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'using': 0.38; 'received:org': 0.38; 'some': 0.38; 'to:addr:python.org': 0.40; 'custom': 0.61; 'skip:o 30': 0.63; 'subject:any': 0.84; '-->': 0.91; 'sort.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Using methodcaller in a list sort - any examples anywhere? Followup-To: gmane.comp.python.general Date: Tue, 13 Dec 2011 17:13:50 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b34f.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 1323792823 news.xs4all.nl 6939 [2001:888:2000:d::a6]:38364 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17145 tinnews@isbd.co.uk wrote: > I want to sort a list of 'things' (they're fairly complex objects) by > the contents of one of the fields I can extract from the 'things' > using a Python function. > > So I have a list L which is a list of objects of some sort. I can > output the contents of a field in the list as follows:- > > for k in L: > print k.get_property('family-name') > > How can I sort the list first? As I said it seems like a methodcaller > is the answer but I don't see how. I want to sort the list of objects > not just produce a sorted list of names. The most obvious way is to use a custom function def get_family_name(obj): return obj.get_property("family-name") L.sort(key=get_family_name) However, since you already know about methodcaller """ class methodcaller(builtins.object) | methodcaller(name, ...) --> methodcaller object | | Return a callable object that calls the given method on its operand. | After, f = methodcaller('name'), the call f(r) returns r.name(). | After, g = methodcaller('name', 'date', foo=1), the call g(r) returns | r.name('date', foo=1). """" L.sort(key=methodcaller("get_property", "family-name"))