[Solved] Sort a list of number by last 2 digits


You can get the last two digits using the remainder operator, then use the digits as key of sorted:

a = [311, 409, 305, 104, 301, 204, 101, 306, 313, 202, 303, 410, 401, 105, 407, 408]
result = sorted(a, key=lambda x: (x % 100, x))
print(result)

Output

[101, 301, 401, 202, 303, 104, 204, 105, 305, 306, 407, 408, 409, 410, 311, 313]

As you want to ties to be solved using the actual value the key is a tuple of the last two digits and the actual value.

1

solved Sort a list of number by last 2 digits