i have a list L = [['92', '022'], ['77', '13'], ['82', '12']]
want to sort on second element as key : ['022','13','12']
having to custom functions for numerically sort and lexicographically sort. but not getting the desired output...
for numerically sort output like : [['82', '12'],['77', '13'],['92', '022']]
for lexicographically sort output like : [['92', '022'],['82', '12'], ['77', '13']]
from functools import cmp_to_key
L = [['92', '022'], ['77', '13'], ['82', '12']]
key=2
def compare_num(item1,item2):
return (int(item1[key-1]) > int(item2[key-1]))
def compare_lex(item1,item2):
return item1[key-1]<item2[key-1]
print(sorted(l, key=cmp_to_key(compare_num)))
print(sorted(l, key=cmp_to_key(compare_lex)))