This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Bug in sort()
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: lucassdssampaio, mark.dickinson, tim.peters
Priority: normal Keywords:

Created on 2018-07-01 19:28 by lucassdssampaio, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg320843 - (view) Author: Lucas Sampaio (lucassdssampaio) Date: 2018-07-01 19:28
lista4 = input().split()
print(lista4)
lista4.sort()
print(lista4)

Input = 6 8 10

Output:
 6 8 10
['6', '8', '10']
['10', '6', '8']

a bug occurs when setting the 10
msg320844 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-07-01 20:06
This isn't a bug: I'm guessing that you expected an output of `['6', '8', '10']`, but in the example you give you're sorting strings rather than numbers, and those strings are sorted lexicographically (i.e., using "dictionary order") as normal.

If you want to do a numeric sort, convert your inputs to numbers first.

>>> lista4 = ['6', '8', '10']
>>> lista4_numbers = [int(s) for s in lista4]
>>> lista4_numbers.sort()
>>> lista4_numbers
[6, 8, 10]
msg320845 - (view) Author: Lucas Sampaio (lucassdssampaio) Date: 2018-07-01 21:00
same code with another input
type is list

lista4 = input().split()
print(lista4)
lista4.sort()
print(lista4)

 6 8 9
['6', '8', '9']
['6', '8', '9']


lista4 = input().split()
print(lista4)
lista4.sort()
print(lista4)

 10 11 12
 
['10', '11', '12']
['10', '11', '12']

but if you put a 9 <with 10

 9 10 11
 
['9', '10', '11']
['10', '11', '9']
msg320846 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2018-07-01 21:05
Lucas, as Mark said you're sorting _strings_ here, not sorting integers.  Please study his reply.  As strings, "10" is less than "9", because "1" is less than "9".

>>> "10" < "9"
True
>>> 10 < 9
False
msg320847 - (view) Author: Lucas Sampaio (lucassdssampaio) Date: 2018-07-01 21:12
ok, I got it

lista4 = input().split()
print(lista4)
lista4.sort()
print(lista4)

print(type(lista4[2]))

 6 8 10['6', '8', '10']
['10', '6', '8']
<class 'str'>

ok, I got it

2018-07-01 18:05 GMT-03:00 Tim Peters <report@bugs.python.org>:

>
> Tim Peters <tim@python.org> added the comment:
>
> Lucas, as Mark said you're sorting _strings_ here, not sorting integers.
> Please study his reply.  As strings, "10" is less than "9", because "1" is
> less than "9".
>
> >>> "10" < "9"
> True
> >>> 10 < 9
> False
>
> ----------
> nosy: +tim.peters
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue34016>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78197
2018-07-01 21:12:20lucassdssampaiosetmessages: + msg320847
2018-07-01 21:05:09tim.peterssetnosy: + tim.peters
messages: + msg320846
2018-07-01 21:00:13lucassdssampaiosetmessages: + msg320845
2018-07-01 20:06:35mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg320844

resolution: not a bug
stage: resolved
2018-07-01 19:28:46lucassdssampaiocreate