My problem is basically that both of this peaces of code work proparly, however, the second solution with key = min doesn't make sence to me. How does python go through this code? Why does it ignore sorting the letters and goes straight to sorting by digits? I can't understand the underlying logics of this code.
My code:
def order(sentence):
return ' '.join(sorted(sentence.split(), key=lambda s: int("".join(filter(str.isdigit, s)))))
print(order("is2 Thi1s T4est 3a"))
Second solution
def order(sentence):
return ' '.join(sorted(sentence.split(), key=min))
print(order("is2 Thi1s T4est 3a"))
The answer is correct in both cases:
Thi1s is2 3a T4est