Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:same': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'algorithm.': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'peters': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reverses': 0.16; 'sorting': 0.16; 'implementing': 0.18; 'stefan': 0.18; 'subject:not': 0.18; 'compare': 0.20; 'elements': 0.23; "python's": 0.23; 'header:In-Reply-To:1': 0.24; 'tim': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'order.': 0.27; 'skip:( 20': 0.28; 'initial': 0.29; 'behaviour': 0.29; 'function:': 0.29; 'relative': 0.31; 'received:84': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'equal': 0.34; 'to:addr:python-list': 0.35; 'easiest': 0.35; 'cases': 0.36; "didn't": 0.37; 'subject:: ': 0.37; 'received:org': 0.38; 'means': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:de': 0.40; 'subject:the': 0.40; 'term': 0.60; 'interested,': 0.61; 'here': 0.66; 'reverse': 0.66 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: Sorting in reverse is not the same as sorting then reversing Date: Fri, 05 Jun 2015 16:50:15 +0200 References: <5571ad0c$0$13005$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-033-235.084.056.pools.vodafone-ip.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <5571ad0c$0$13005$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433515834 news.xs4all.nl 2969 [2001:888:2000:d::a6]:50302 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92155 Steven D'Aprano schrieb am 05.06.2015 um 16:07: > Sorting in reverse does not give the same result as sorting then reversing. > > It's easiest to see with a key function: > > py> a = ['fox', 'dog', 'DOG', 'cat', 'ape'] > py> b = a[:] > py> a.sort(key=str.lower, reverse=True) > py> b.sort(key=str.lower) > py> b.reverse() > py> a > ['fox', 'dog', 'DOG', 'cat', 'ape'] > py> b > ['fox', 'DOG', 'dog', 'cat', 'ape'] > > Sorting in reverse keeps the initial order of any equal elements unchanged. > Sorting, then reversing, reverses them. > > (Thanks to Tim Peters for the tip.) ... and for implementing this in the first place. :) For those of you who didn't know and now got interested, the relevant term here is "stable sorting". It means that elements that compare equal keep their relative order. That's a general property of Python's sort algorithm. All that "reverse=True" does is to change "lower than" into "greater than" and vice versa for elements that compare unequal. It does not change the behaviour for elements that compare equal, which means that they keep the same relative order in both cases (reversed/non-reversed). Stefan