Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*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; 'peters': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'reverses': 0.16; 'sorting': 0.16; 'wrote:': 0.16; 'subject:not': 0.18; 'elements': 0.23; 'tim': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'initial': 0.29; 'function:': 0.29; "d'aprano": 0.33; 'steven': 0.33; 'equal': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'easiest': 0.35; 'there': 0.36; 'subject:: ': 0.37; 'received:org': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:de': 0.40; 'subject:the': 0.40; 'reverse': 0.66 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Sorting in reverse is not the same as sorting then reversing Date: Fri, 05 Jun 2015 16:59:50 +0200 Organization: None References: <5571ad0c$0$13005$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8ccb.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433516413 news.xs4all.nl 2905 [2001:888:2000:d::a6]:53803 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92156 Steven D'Aprano wrote: > 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. If there were no reverse flag you could reverse, then sort, then reverse again. > (Thanks to Tim Peters for the tip.)