Below is my code for for sorting the list.
def sorthelper(x):
return x[1]
ls=[('data2',1),('data1',3),('data3',2)]
print(sorted(ls,key=sorthelper))
Here I am able to define custom key and accordingly list is sorted and got the expected result.
What keeps intriguing me that how sorted()
function is working internally with key to sort the list ? How value of variable key
is used for sorting ?
My understanding is sorthelper()
(defined by me for returing key) is getting called multiple time and returning the key
Is variable key
is maintaining some sort of list of all returned keys to sort original list ? If yes then requesting to explain the working as well.
I have tried to search as much as possible but didn't found any such explanation of my question.
Also Please bear with me for my bad English writing. Thanks for the understanding.