X Tutup
The Wayback Machine - https://web.archive.org/web/20220625201049/https://github.com/python/cpython/issues/94271
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() returns the wrong result for a specific input using the key parameter #94271

Open
jweyrich opened this issue Jun 25, 2022 · 0 comments
Open
Labels
type-bug

Comments

@jweyrich
Copy link

@jweyrich jweyrich commented Jun 25, 2022

Bug report

The sorted() function is returning the wrong result for a specific input when using the key parameter. Here's my test case:

def test(input: list, expected: list):
    indices = sorted(range(len(input)), key=input.__getitem__)
    print(f'{indices == expected}, indices={indices}, expected={expected}')

test([1,2,3,4,5,6], [0,1,2,3,4,5]) # Output: True, indices=[0, 1, 2, 3, 4, 5], expected=[0, 1, 2, 3, 4, 5]
test([1,2,3,4,6,5], [0,1,2,3,5,4]) # Output: True, indices=[0, 1, 2, 3, 5, 4], expected=[0, 1, 2, 3, 5, 4]
test([1,2,3,6,5,4], [0,1,2,5,4,3]) # Output: True, indices=[0, 1, 2, 5, 4, 3], expected=[0, 1, 2, 5, 4, 3]
test([3,2,1,5,6,4], [2,1,0,4,5,3]) # Output: False, indices=[2, 1, 0, 5, 3, 4], expected=[2, 1, 0, 4, 5, 3]

The 4th (last) test prints False because it is not sorting the input correctly. Looks like a bug, or I'm missing something here.

Your environment

  • CPython versions tested on: 3.8.2 and 3.8.5
  • Operating system and architecture: macOS 10.15.7 and WSL 2 Ubuntu 20.04 LTS
@jweyrich jweyrich added the type-bug label Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug
Projects
None yet
Development

No branches or pull requests

1 participant
X Tutup