2

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.

novice
  • 394
  • 2
  • 11
  • Have you seen [this](https://docs.python.org/3/library/functions.html#sorted)? – shree.pat18 Jun 14 '22 at 03:24
  • @shree.pat18 I have checked now. I have not found any explanation how key is actually helpful in sorting. Thanks for the response though. – novice Jun 14 '22 at 03:39
  • Check out: [Sorting HOW TO](https://docs.python.org/3/howto/sorting.html) – DarrylG Jun 14 '22 at 03:41
  • @DarrylG , Looks like I have failed to make question understandable . Still I am struggling to find how actually key is helping to inbuilt sorted function.Let me put it in an another way . Why list is not getting sorted when key returing function `sorthelper()` returning any fix value , let say the value is 9 ? how this returned key is getting compared to each item in the list while sorting process is being performed. – novice Jun 14 '22 at 04:05
  • 1
    "Why list is not getting sorted when key returing function sorthelper() returning any fix value" -- the answer is Python uses a stable sort algorithm meaning if two items have the same key, their order will be preserved in the sorted list. – DarrylG Jun 14 '22 at 04:30
  • @DarrylG How python compares the key with each item ? – novice Jun 14 '22 at 04:31
  • 1
    Without a key funciton to compare elements in list `arr` at indexes i & j we have `arr[i] >= arr[j]`. With the key function, say function `keyfunc`, we pass values through the key function before the comparison to get: `keyfunc(arr[i]) >= keyfunc(arr[j])`. An example is my modification of quicksort to add a key function as shown by [Using quick sort to sort elements by frequency: how can I order elements of same frequency by size](https://stackoverflow.com/questions/67280332/using-quick-sort-to-sort-elements-by-frequency-how-can-i-order-elements-of-same/67281248#67281248) – DarrylG Jun 14 '22 at 09:29
  • @DarrylG That's rather misleading, especially since they've already wondered about "getting called multiple time" and "list of all returned keys". – Kelly Bundy Jun 14 '22 at 16:57
  • @KellyBundy -- please expand on how it's misleading. I was only mentioning the key function is applied in the comparison operation. The number of times is controlled by many comparisons are done. Efficient algorithms like quicksort perform O(n*log(n)) on average comparisons. This makes the number of times the key function is called in these algorithms also on the order of O(n*log(n)). – DarrylG Jun 14 '22 at 17:04
  • 1
    @DarrylG Oh so you actually don't know... The key function is called exactly n times, once for each element. Before the actual sorting begins. The keys are stored and those stored keys are used during the actual sorting (instead of the key function). – Kelly Bundy Jun 14 '22 at 17:14
  • 1
    @KellyBundy -- thanks for pointing that out. So, in my quicksort example code I should have first redefined the input array as `arr = [keyfunc(v) for v in arr]`, then performed a quicksort on the revised arr. Good to know. Thanks. – DarrylG Jun 14 '22 at 17:37
  • 1
    @DarrylG Something like that, yes. And then both the values array and the keys array get sorted in parallel (by key). I.e., the keys array gets sorted and all moves are applied to the values array as well. – Kelly Bundy Jun 14 '22 at 18:03
  • Hi @KellyBundy, Can you please help to know how sorted key iist help to sort origingal list? – novice Aug 08 '22 at 16:01

0 Answers0