Path: csiph.com!weretis.net!feeder4.news.weretis.net!storethat.news.telefonica.de!feedme.news.telefonica.de!telefonica.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Sorting a list Date: Mon, 04 Apr 2016 16:12:13 +0200 Organization: None Lines: 25 Message-ID: References: <1459777242.3422585.568322458.4B66944D@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de ioBy7IvxmGye1MZ8hgkXBgtcsmPrRbx1mxpL1bhR+NwQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'type,': 0.07; 'wrapper': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; "'b',": 0.16; "'c',": 0.16; "'d',": 0.16; '__lt__(self,': 0.16; 'key):': 0.16; 'negation': 0.16; 'orderable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'reversed': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'second': 0.24; 'import': 0.24; 'mon,': 0.24; 'header :User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints- To:1': 0.26; 'skip:_ 10': 0.32; 'class': 0.33; 'though.': 0.33; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'thought': 0.37; 'why': 0.39; 'does': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd80fb.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <1459777242.3422585.568322458.4B66944D@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:106441 Random832 wrote: > On Mon, Apr 4, 2016, at 02:56, Peter Otten wrote: >> > That works well. Why is it 'cheating'? >> >> On second thought it isn't ;) > > It does require a numeric type, though. There are lots of types that are > orderable but do not have a negation operator that provides a key with > reversed ordering. If you are willing to accept the overhead you can use a wrapper class: >>> import functools >>> @functools.total_ordering ... class neg: ... def __init__(self, key): ... self.key = key ... def __lt__(self, other): ... return other.key < self.key ... >>> sorted("abcde", key=neg) ['e', 'd', 'c', 'b', 'a']