Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sorted ignores reverse=True when sorting produces same list #73940

Closed
TomasDabainskas mannequin opened this issue Mar 8, 2017 · 3 comments
Closed

sorted ignores reverse=True when sorting produces same list #73940

TomasDabainskas mannequin opened this issue Mar 8, 2017 · 3 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@TomasDabainskas
Copy link
Mannequin

TomasDabainskas mannequin commented Mar 8, 2017

BPO 29754
Nosy @tim-one

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-03-08.06:07:53.690>
created_at = <Date 2017-03-08.05:32:52.115>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = 'sorted ignores reverse=True when sorting produces same list'
updated_at = <Date 2017-03-08.08:16:03.101>
user = 'https://bugs.python.org/TomasDabainskas'

bugs.python.org fields:

activity = <Date 2017-03-08.08:16:03.101>
actor = 'Tomas Daba\xc5\xa1inskas'
assignee = 'none'
closed = True
closed_date = <Date 2017-03-08.06:07:53.690>
closer = 'tim.peters'
components = ['Library (Lib)']
creation = <Date 2017-03-08.05:32:52.115>
creator = 'Tomas Daba\xc5\xa1inskas'
dependencies = []
files = []
hgrepos = []
issue_num = 29754
keywords = []
message_count = 3.0
messages = ['289202', '289204', '289217']
nosy_count = 2.0
nosy_names = ['tim.peters', 'Tomas Daba\xc5\xa1inskas']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue29754'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7']

@TomasDabainskas
Copy link
Mannequin Author

TomasDabainskas mannequin commented Mar 8, 2017

sorted ignores reverse=True when sorting produces same list, I was expecting reverse regardless of the sorting outcome.

Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = [{'name': 'first', 'weight': 1},{'name': 'second', 'weight': 1},{'name': 'third', 'weight': 1}, {'name': 'fourth', 'weight': 1}]
>>> sorted(data, key=lambda x: x['weight'], reverse=True)
[{'name': 'first', 'weight': 1}, {'name': 'second', 'weight': 1}, {'name': 'third', 'weight': 1}, {'name': 'fourth', 'weight': 1}]
>>> sorted(data, key=lambda x: x['weight'], reverse=True) == sorted(data, key=lambda x: x['weight']).reverse()
False

Thanks!

@TomasDabainskas TomasDabainskas mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir labels Mar 8, 2017
@tim-one
Copy link
Member

tim-one commented Mar 8, 2017

Your last line can't possibly return True, because somelist.reverse() returns None.

So the last line is irrelevant. Your complaint appears to be about the line before, which shows that the list retains its original order.

That's expected. All the keys are equal, so a stable sort _must_ retain the original order (that's what "stable" means). So that's not a bug - it's a feature.

The idea that x.sort(reverse=True) must do the same as x.sort(); x.reverse() is something you made up in your head ;-) That is, the docs don't say that. What they do say:

"""
reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
"""

That has no effect on keys that compare equal. It means that keys that compare "less than" are treated as if they had compared "greater than" instead, and vice versa.

While it may not be immediately obvious, what x.sort(reverse=True) is actually equivalent to is the sequence:

x.reverse()
x.sort()
x.reverse()

@tim-one tim-one closed this as completed Mar 8, 2017
@tim-one tim-one added invalid type-bug An unexpected behavior, bug, or error labels Mar 8, 2017
@TomasDabainskas
Copy link
Mannequin Author

TomasDabainskas mannequin commented Mar 8, 2017

Thanks for taking time to review and respond Tim! (;

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant