[issue35010] sort by partially reversed key tuple

2018-10-18 Thread Cyker Way
Cyker Way added the comment: Thank you very much for the great discussion here, especially Tim's great threads in *python-ideas* that give neat and insightful answers to this problem in different ways: - -

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Would be worth to add a wrapper in functools which revert the sorting order? class reverted_order: def __init__(self, value): self.value = value def __lt__(self, other): if isinstance(other, reverted_order): other = other.

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: Sorry to comment on the closed issue. I think the multisort recipe [0] is pretty neat can be added to the sorting howto page instead of being lost in the mailing list thread. It was suggested for addition later in the thread but never got added. I

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Tim Peters
Tim Peters added the comment: This comes up every few years, but that's about it. Here's the iteration from 2 years ago: https://mail.python.org/pipermail/python-ideas/2016-October/043039.html Follow the thread. It contains easy-to-use wrappers for both "do it in multiple simple passes" a

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Raymond Hettinger
Change by Raymond Hettinger : -- stage: patch review -> resolved status: open -> closed ___ Python tracker ___ ___ Python-bugs-list

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Raymond Hettinger
Raymond Hettinger added the comment: Cyker, thank you for the suggestion, but we're going to decline. The sorting HOWTO docs show how to exploit sort stability with multiple passes to handle a mix of ascending and descending steps. It would complicate the API to have an array of key funct

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Steven D'Aprano
Steven D'Aprano added the comment: > And you are free to use whatever sorting algorithms in its implementation for > this kind of task. That's very kind of you *wink* At this point, I don't think there's much more point to discussing this further until Tim Peters weighs in and lets us know

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Cyker Way
Cyker Way added the comment: As for performance, I think both single-pass and multi-pass sorts have worst-case time complexity `m * n * log(n)`, assuming the number of items is `n` and each item has dimension `m`. Whichever is faster seems to be data-dependent. So I made a more comprehensive

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Cyker Way
Cyker Way added the comment: Previous upload `example.py` was missing `__eq__`. Updated in `example-1.py`. -- Added file: https://bugs.python.org/file47876/example-1.py ___ Python tracker ___

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- keywords: +patch pull_requests: +9282 stage: -> patch review ___ Python tracker ___ ___ Pyt

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Steven D'Aprano
Steven D'Aprano added the comment: The ``sorted`` docs links to the Sorting HOWTO, the ``list.sort`` docs should do the same. https://docs.python.org/3/library/functions.html#sorted https://docs.python.org/3/library/stdtypes.html#list.sort -- ___

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Cyker Way
Cyker Way added the comment: Multi-pass stable sorts should produce the correct result. But as the number of columns grow the code gets messy. For brevity this example only has 2 columns but it may be 10 or more in a real application. Furthermore, in some cases the application may need to re

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: @Serhiy I just checked the docs to add an example and it's explained with an example at [0] :) ``` Sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved. This wonderful prop

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Would be nice to add an example in the documentation. -- nosy: +serhiy.storchaka ___ Python tracker ___ ___

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: There was some discussion about it : https://lists.gt.net/python/python/539896#539896 . As suggested by Raymond in the thread the below can be used to get the desired output items.sort(key=lambda r: r['user'], reverse=True) items.sort(key=lambda r:

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Steven D'Aprano
Steven D'Aprano added the comment: Since sort is guaranteed to be stable, can't you sort in two runs? py> values = ['bc', 'da', 'ba', 'abx', 'ac', 'ce', 'dc', 'ca', 'aby'] py> values.sort(key=itemgetter(1), reverse=True) py> values.sort(key=itemgetter(0)) py> values ['ac', 'abx', 'aby', 'bc',

[issue35010] sort by partially reversed key tuple

2018-10-17 Thread Cyker Way
New submission from Cyker Way : The current `sorted` function is somewhat limited and doesn't cover a use case that frequently occurs in real applications: sort by a tuple of keys where each key can be in asc or desc order. For example, you may have a list of site configs where each of specif