Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python sort put upper case items first?

Not looking for a work around. Looking to understand why Python sorts this way.

>>> a = ['aaa','Bbb']
>>> a.sort()
>>> print(a)
['Bbb', 'aaa']

>>> a = ['aaa','bbb']
>>> a.sort()
>>> print(a)
['aaa', 'bbb']
like image 269
Matt Avatar asked Jan 18 '19 03:01

Matt


People also ask

Do capital letters come first in Python?

Case-insensitive Sorting By default, the sort() method sorts the list in ASCIIbetical order rather than actual alphabetical order. This means uppercase letters come before lowercase letters.

How does pythons sort function work?

sort() method sorts the elements of a list in ascending or descending order using the default < comparisons operator between items. Use the key parameter to pass the function name to be used for comparison instead of the default < operator. Set the reverse parameter to True, to get the list in descending order.

What is the default sorting order in Python?

Since the name is a string , Python by default sorts it using the alphabetical order. For the second case, age ( int ) is returned and is sorted in ascending order. For the third case, the function returns the salary ( int ), and is sorted in the descending order using reverse = True .

Why do we use upper () in Python?

To convert any character into uppercase in Python, we use the built-in upper() function. Python upper() function is an inbuilt string class method that converts all the lowercase characters in the string into uppercase characters and returns a new string.


3 Answers

This is because upper case chars have an ASCII value lower than that of lower case. And hence if we sort them in increasing order, the upper case will come before the lower case

  • ASCII of A is 65
  • ASCII of a is 97

65<97

And hence A < a if you sort in increasing order

like image 148
mrid Avatar answered Oct 17 '22 08:10

mrid


str is sorted based on the raw byte values (Python 2) or Unicode ordinal values (Python 3); in ASCII and Unicode, all capital letters have lower values than all lowercase letters, so they sort before them:

>>> ord('A'), ord('Z')
(65, 90)
>>> ord('a'), ord('z')
(97, 112)

Some locales (e.g. en_US) will change this sort ordering; if you pass locale.strxfrm as the key function, you'll get case-insensitive sorts on those locales, e.g.

>>> import locale
>>> locale.setlocale(locale.LC_COLLATE, 'en_US.utf-8')
>>> a.sort(key=locale.strxfrm)
>>> a
['aaa', 'Bbb']
like image 26
ShadowRanger Avatar answered Oct 17 '22 07:10

ShadowRanger


Python treats uppercase letters as lower than lowercase letters. If you want to sort ignoring the case sensitivity. You can do something like this:

a = ['aaa','Bbb']
a.sort(key=str.lower)
print(a)

Outputs:
['aaa', 'Bbb']

Which ignores the case sensitivity. The key parameter "str.lower" is what allows you to do this. The following documentation should help. https://docs.python.org/3/howto/sorting.html

like image 28
farstop Avatar answered Oct 17 '22 09:10

farstop