I was looking a lot and reading a lot of question, but I cannot figure out how to give two arguments to the key of sort method, so I can make a more complex comparison.
Example:
class FruitBox():
def __init__(self, weigth, fruit_val):
self.weigth = weigth
self.fruit_val = fruit_val
I want to compare the FruitBox by fruit_val, but! Also they box heavier are bigger than others.
So it would be:
f1 = FruitBox(2,5)
f2 = FruitBox(1,5)
f3 = FruitBox(2,4)
f4 = FruitBox(3,4)
boxes = [f1,f2,f3,f4]
boxes.sort(key = ???) # here is the question
Expected result:
=> [FruitBox(2,4),FruitBox(3,4),FruitBox(1,5),FruitBox(2,5)]
Is there a way to send a function with 2 arguments, when I do it something like
def sorted_by(a,b):
#logic here, I don't know what will be yet
and I do
boxes.sort(key=sorted_by)
It throws:
Traceback (most recent call last):
File "python", line 15, in <module>
TypeError: sort_by_b() missing 1 required positional argument: 'b'
How can I give Two Arguments to the key of sort?