Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'wed,': 0.04; 'implicit': 0.07; 'append': 0.09; 'from:addr:python': 0.09; 'kelly': 0.09; 'subject:method': 0.09; 'pm,': 0.11; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '"a"': 0.16; "'')": 0.16; '(0,': 0.16; '(1,': 0.16; '1),': 0.16; '1:08': 0.16; '3),': 0.16; '5)]': 0.16; 'fix:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'mrab': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:mrabarnett.plus.com': 0.16; 'reply-to:addr:python-list': 0.16; 'sorting': 0.16; 'subject:sort': 0.16; 'tue,': 0.20; 'header:In-Reply-To:1': 0.22; 'received:84': 0.25; 'chris': 0.27; 'url:mailman': 0.27; 'class': 0.29; 'probably': 0.30; 'list': 0.30; 'character.': 0.31; 'comparison': 0.31; 'wraps': 0.31; 'to:addr:python-list': 0.32; 'another': 0.32; "i've": 0.33; 'url:listinfo': 0.33; 'example,': 0.33; 'character': 0.33; "isn't": 0.34; 'header:User-Agent:1': 0.35; 'reply-to:addr:python.org': 0.35; 'quite': 0.36; 'getting': 0.36; 'think': 0.36; 'url:python': 0.37; 'strings': 0.38; 'less': 0.38; 'but': 0.38; 'url:org': 0.38; 'end': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'greater': 0.40; 'would': 0.40; "it's": 0.40; '2011': 0.62; 'mar': 0.64; 'reply- to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; '30,': 0.80; '...where': 0.84; '12:48': 0.84; 'bigger,': 0.84; 'subject:removal': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqkHAHRlkk1UXebj/2dsb2JhbACYUI0Bd70yhWoElAE Date: Wed, 30 Mar 2011 00:06:51 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Guido rethinking removal of cmp from sort method References: <20110323151426.GB5668@trout.vub.ac.be> <20110324092323.GC5668@trout.vub.ac.be> <4d8bd8a1$0$29977$c3e8da3$5496439d@news.astraweb.com> <4d8d1203$0$29977$c3e8da3$5496439d@news.astraweb.com> <4d90ac87$0$30000$c3e8da3$5496439d@news.astraweb.com> <20110329084657.GE26597@trout.vub.ac.be> <4D922B91.5030505@mrabarnett.plus.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 54 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1301440013 news.xs4all.nl 41114 [::ffff:82.94.164.166]:40994 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2198 On 29/03/2011 21:32, Dan Stromberg wrote: > > On Tue, Mar 29, 2011 at 12:48 PM, Ian Kelly > wrote: > > On Tue, Mar 29, 2011 at 1:08 PM, Chris Angelico > wrote: > > On Wed, Mar 30, 2011 at 5:57 AM, MRAB > wrote: > >> You would have to do more than that. > >> > >> For example, "" < "A", but if you "negate" both strings you get "" < > >> "\xBE", not "" > "\xBE". > > > > Strings effectively have an implicit character at the end that's less > > than any other character. Easy fix: Append a character that's greater > > than any other. So "" < "A" becomes "\xFF" > "\xBE\xFF". > > > > Still not going to be particularly efficient. > > Not to mention that it still has bugs: > > "" < "\0" > "\xff" < "\xff\xff" > -- > http://mail.python.org/mailman/listinfo/python-list > > > It probably could be a list of tuples: > > 'abc' -> [ (1, chr(255 - ord('a')), (1, chr(255 - ord('b')), (1, chr(255 > - ord('c')), (0, '') ] > > ...where the (0, '') is an "end of string" marker, but it's getting > still slower, and quite a lot bigger, and getting so reductionistic > isn't a good thing when one class wraps another wraps another - when > their comparison methods are interrelated. > I think I've found a solution: class NegStr: def __init__(self, value): self._value = value def __lt__(self, other): return self._value > other._value so when sorting a list of tuples: def make_key(t): return NegStr(t[0]), t[1] items = [("one", 1), ("two", 2), ("three", 3), ("four", 4), ("five", 5)] items.sort(key=make_key)