0

I was wondering, how is value of item1 and item2 considered?

import functools

lst = [ 2, 1, 3, 6,0, 4, 5]

def compare(item1, item2):
    print item1, item2
    return (item1) - (item2)


print lst
sorted(lst, key=functools.cmp_to_key(compare))

The output is given below

[2, 1, 3, 6, 0, 4, 5]
1 2    
3 1
3 2
6 2
6 3
0 3
0 2
0 1
4 2
4 6
4 3
5 3
5 6
5 4
Out[25]: [0, 1, 2, 3, 4, 5, 6]

I want to know how and on what bases are the values of item1 and item2 are considered?

martineau
  • 119,623
  • 25
  • 170
  • 301
Rajat Raj
  • 25
  • 1
  • 2
    Have you checked [the documentation](https://docs.python.org/3/library/functools.html?highlight=functools%20cmp_to_key#functools.cmp_to_key)? It explains what an old-style comparison function is, what a key function is, and how `functools.cmp_to_key` converts between the two. – Adam Smith Nov 26 '17 at 02:18
  • @AdamSmith I have read it. It only mentions that two arguments are taken and then compared. My question is how are those two arguments taken? – Rajat Raj Nov 26 '17 at 02:21
  • For a look at how `cmp_to_key` actually works, see [here](https://stackoverflow.com/questions/16362744/how-does-pythons-cmp-to-key-function-work). – Sebastian Mendez Nov 26 '17 at 02:22
  • 1
    @RajatRaj what do you mean? Algorithmically? Python implements [Timsort](https://en.wikipedia.org/wiki/Timsort) so-named after its developer [Tim Peters](https://stackoverflow.com/users/2705542/tim-peters) – Adam Smith Nov 26 '17 at 02:22
  • @AdamSmith Yes, I know python implements sorting using timsort. When we pass a custom function to a sort function, it considers passing two elements at a time as a argument to it. I have displayed the values of `item1` and `item2` in my question. I want to know how these elements are considered. – Rajat Raj Nov 26 '17 at 02:28
  • 1
    @RajatRaj your use of the word "considered" here is unclear. They are compared using the comparator function that you defined as `compare`, having run through `functool.cmp_to_key` whose definition you said you read. If you're asking why it's comparing the two elements it's comparing and why in that order, well, I thought you said you knew that Python implements Timsort, because that's the algorithm it's following. Take a half hour and read the two links I left you again, because they answer all the questions you could possibly have about this topic. – Adam Smith Nov 26 '17 at 02:30
  • @AdamSmith Thanks you for the help. I will definitely consider reading them. – Rajat Raj Nov 26 '17 at 02:32
  • (there's that word again...) – Adam Smith Nov 26 '17 at 02:33
  • How the values of item1 and item2 are considered is determined by your comparison function as indicated by it returns, so it can more-or-less be anything a function can do—what part(s) of the process don't you understand? – martineau Nov 26 '17 at 02:34
  • @martineau In the first operation `1` and `2` is passed, in the second `3` and `1`, in the 3rd `3` and `2`. My question is how is on what rule are these values getting passed? – Rajat Raj Nov 26 '17 at 02:38
  • 1
    For details like that, you'll need to research how Python's hybrid Timsort works. There's an [article](https://en.wikipedia.org/wiki/Timsort) on it in Wikipedia. There's links to the article in Python's documentation (here's [one of them](https://docs.python.org/3/howto/sorting.html?highlight=timsort#sort-stability-and-complex-sorts)). – martineau Nov 26 '17 at 02:46
  • To anyone reading this far, it's not mentioned in the documentation (that I know of) but you can use `functools.cmp_to_key` as a function @decorator because of the way it's written—which makes using it even easier. – martineau Nov 26 '17 at 02:56
  • 1
    The exact values being compared are implementation details of the sorting algorithm that are subject to change and shouldn't matter to you. If you really need to understand it to that level of detail you should read the existing documentation as @martineau suggests. Those details are outside the scope of a StackOverflow answer. – Mark Ransom Nov 26 '17 at 03:01

0 Answers0