Feb-06-2017, 04:47 PM
I wonder how to make sorting with 'cmp' work in Python 2.7.
It's not my case, but as an example, how could I sort sets like
Obviously, I can't sort them alphabetically or by word length. I need to give an order in the kind of Monday < Tuesday < Wednesday < Thursday < Friday < Saturday < Sunday.
The python docs write: "cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower())." Now I have more then two arguments (in the weekdays example, there are 7), and I would prefer a way without lambda, if that's possible. I seem to remember that a function could be used?
It's not my case, but as an example, how could I sort sets like
[Wednesday, Tuesday, Saturday, Monday, Sunday], [Monday, Tuesday, Friday, Wednesday], [Sunday, Saturday, Sunday], [Friday, Tuesday, Thursday]in the weekdays order?
Obviously, I can't sort them alphabetically or by word length. I need to give an order in the kind of Monday < Tuesday < Wednesday < Thursday < Friday < Saturday < Sunday.
The python docs write: "cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower())." Now I have more then two arguments (in the weekdays example, there are 7), and I would prefer a way without lambda, if that's possible. I seem to remember that a function could be used?