The sort key should do: "split once, convert to integer". Not converting to integer fails because then lexicographical compare is used and in that case "10" < "2"
, not that you want.
l = ['345345:player7',
'435:zo',
'345:name',
'23:hello',
'1231:football']
result = sorted(l, key=lambda x: int(x.split(':',1)[0]))
result:
['23:hello', '345:name', '435:zo', '1231:football', '345345:player7']
that doesn't handle the tiebreaker where numbers are equal. A slightly more complex sort key would be required (but still doable). In that case, drop lambda
and create a real function so you can perform split once & unpack to convert only the first part to integer:
def sortfunc(x):
number,rest = x.split(':',1)
return int(number),rest
result = sorted(l, key=sortfunc)