Can anyone explain how this is sorted like this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain how this is sorted like this

https://code.sololearn.com/caFFt427eK1C/?ref=app

31st Oct 2020, 11:01 PM
Adebisi Olawale
Adebisi Olawale - avatar
2 Answers
+ 5
It is comparing each character in a string by its ascii value , strings are compared character by character ,so first of each one is checked and so P is smaller than f and i ,and f is smaller than i and we get a sorted list in ascending order ,
31st Oct 2020, 11:12 PM
Abhay
Abhay - avatar
+ 2
You can change the sort criteria by passing a function or lambda as the key argument to the sort() method. For instance this will sort the list by the length of the str elements. words.sort(key=lambda x: len(x))
1st Nov 2020, 3:50 AM
ChaoticDawg
ChaoticDawg - avatar