Sort given 2d array in order of ascending by python3.x

Solution 1:

Try this you need to flatten your list into single list like this

tmp = [t for x in input_arr for t in x]

then do the sorting based on first element before the -, like this

print(list(sorted(tmp,key=lambda x: int(x.split('-')[0]))))

this will give you your desired output.

['11-86', '16-75', '22-58', '23-53', '36-84', '48-15', '48-6', '55-29', '55-32', '55-11', '55-9', '62-3', '71-32', '72-80', '72-10', '73-23', '84-38', '93-51', '93-49', '93-66']

for second condition you can try

print(list(sorted(tmp,key=lambda x: (int(x.split('-')[0]) , int(x.split('-')[1])))))

this will give you

['11-86', '16-75', '22-58', '23-53', '36-84', '48-6', '48-15', '55-9', '55-11', '55-29', '55-32', '62-3', '71-32', '72-10', '72-80', '73-23', '84-38', '93-49', '93-51', '93-66']