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


Groups > comp.lang.python > #106441

Re: Sorting a list

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Sorting a list
Date 2016-04-04 16:12 +0200
Organization None
Message-ID <mailman.19.1459779546.32530.python-list@python.org> (permalink)
References (1 earlier) <mailman.408.1459711883.28225.python-list@python.org> <ndrt29$36l$1@dont-email.me> <ndt36r$oi$1@ger.gmane.org> <1459777242.3422585.568322458.4B66944D@webmail.messagingengine.com> <ndtsnu$s0a$1@ger.gmane.org>

Show all headers | View raw


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']

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


Thread

Sorting a list DFS <nospam@dfs.com> - 2016-04-03 14:30 -0400
  Re: Sorting a list DFS <nospam@dfs.com> - 2016-04-03 14:34 -0400
  Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-03 21:31 +0200
    Re: Sorting a list DFS <nospam@dfs.com> - 2016-04-03 16:08 -0400
      Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-04 08:56 +0200
      Re: Sorting a list Random832 <random832@fastmail.com> - 2016-04-04 09:40 -0400
      Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-04 16:12 +0200

csiph-web