Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92156

Re: Sorting in reverse is not the same as sorting then reversing

From Peter Otten <__peter__@web.de>
Subject Re: Sorting in reverse is not the same as sorting then reversing
Date 2015-06-05 16:59 +0200
Organization None
References <5571ad0c$0$13005$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.203.1433516413.13271.python-list@python.org> (permalink)

Show all headers | View raw


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.)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sorting in reverse is not the same as sorting then reversing Steven D'Aprano <steve@pearwood.info> - 2015-06-06 00:07 +1000
  Re: Sorting in reverse is not the same as sorting then reversing Stefan Behnel <stefan_ml@behnel.de> - 2015-06-05 16:50 +0200
  Re: Sorting in reverse is not the same as sorting then reversing Peter Otten <__peter__@web.de> - 2015-06-05 16:59 +0200
  Re: Sorting in reverse is not the same as sorting then reversing Skip Montanaro <skip.montanaro@gmail.com> - 2015-06-05 10:28 -0500

csiph-web