Python Forum

Full Version: sorting a 2 dimensional array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Folks,

i want to sort an array by numbers, like this array for example


[['B', '-50', '-30', '-8', '1000'], 
    ['T', '-60', '-180', '0', 'parts.dat'], 
    ['T', '-160', '-160', '0', '40']]
Now I want to sort the array by the second or third character, any of you has a idea how to?
I tried this code to sort the array, but now I have the problem that it doesn't work with the letters in my data.


 data = [[int(c) for c in row] for row in data]
    data = sorted(tokenized, key=lambda x: x[1])
    print(data)
Hope anyone can help me with this :)

Kind reguards,

M.D.B.
a = [['B', '-50', '-30', '-8', '1000'],
    ['T', '-60', '-180', '0', 'parts.dat'],
    ['T', '-160', '-160', '0', '40']]

a.sort(key=lambda k: int(k[1]))
for i in a:
    print(i)

sort by third and then second
a.sort(key=lambda k: (int(k[2]), int(k[1])))