I need to sort an array of arrays by a specific element
This is an array:
arr=
[0, [71, 554, 258, 793]]
[1, [61, 415, 148, 593]]
[2, [91, 145, 658, 893]]
I need to be able to sort it by arr[0][0]
as well as by any element from the internal array like arr[0][1]
or arr[0][2]
currently, I'm able to sort it by using key=itemgetter(1)
where: itemgetter(1)
- is second element of array [0, [71, 554, 258, 793]]
in this cese = 71
from operator import itemgetter
array = sorted(array, key=itemgetter(1))
print(*array[:], sep="\n")
how to sort this array by any element from the internal array [71, 554, 258, 793]
?
so if I'm sorting by the second element from internal array output should be like this: (column 145, 415, 554)
arr=
[2, [91, 145, 658, 893]]
[1, [61, 415, 148, 593]]
[0, [71, 554, 258, 793]]
if I'm sorting by the third element from internal array output should be like this: (column 148, 258, 658)
arr=
[1, [61, 415, 148, 593]]
[0, [71, 554, 258, 793]]
[2, [91, 145, 658, 893]]
this is kinda similar to this question: Python Sort Multidimensional Array Based on 2nd Element of Subarray