l=[[1,4],
[2,7],
[10,1],
[1,2],
[10,6],
[2,1]]
print(sorted(l,key=lambda x: (x[0],x[1]))) # use lambda to sort by "x[0]"-> first element of the sublists or x[1] -> second element, if its a tie
[[1, 2], [1, 4], [2, 1], [2, 7], [10, 1], [10, 6]]
Or simply sorted(l) of l.sort() as your elements sort naturally.
A better example would be to sort by the second value only:
print(sorted(l,key=lambda x: (x[1])))
[[10, 1], [2, 1], [1, 2], [1, 4], [10, 6], [2, 7]]
Answer from Padraic Cunningham on Stack Overflowpython - sort a 2D list first by 1st column and then by 2nd column - Stack Overflow
Sorting 2D List in Python - Stack Overflow
How would you sort a 2D list by row based on the first column using a separate list as a key
python - How can I sort a 2D list? - Stack Overflow
Do not forget to put commas between the elements of a list
myVar = [['blueberries','fruit','5.20'],
['bean sprouts','vegetable','9.25'],
['tulip','flower','8.93']]
Now you could use the sorted builtin as Andy Knight suggested in the comments and specify a key function to use for comparing items.
Sort list by price and get a copy of the sorted list
sorted(myVar, key=lambda x: float(x[2]))
Sort list by item name
sorted(myVar, key=lambda x: x[0])
Consider using something like this:-
myList = [['blueberries', 'fruit', 5.20], [
'bean sprouts', 'vegetable', 9.25], ['tulip', 'flower', 8.93]]
for i in range(3):
print(sorted(myList, key=lambda x: x[i]))
so i have a data set of rain data over the course of a year where it ends up being formatted into something like this except its a lot longer
[['Nov', 0.0], ['Nov', 0.0], ['Nov', 0.09],['Feb', 0.0], ['Feb', 0.0], ['Feb', 0.09],['Oct', 0.56], ['Oct', 0.0], ['Oct', 0.03], ['Oct', 0.62]]
now I figured I would need another list to function as a lookup to help sort this data by month according to calendar order so I made one
month_lookup = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
but now I am a tad confused about how I would go about using something like the sorted() function to use the lookup as the way to sort the 2D list above
zip() the 2D list, sort by the first item, then zip() again.
>>> table = [['D', 'C', 'B', 'A',],
... [1, 3, 2, 0,],
... [1, 3, 2, 0],
... [1, 3, 2, 0]]
>>> for row in zip(*sorted(zip(*table), key=lambda x: x[0])):
... print(*row)
...
A B C D
0 2 3 1
0 2 3 1
0 2 3 1
Assume values stored row-by-row in list, like that:
a = [['D', 'C', 'B', 'A'],
['1', '3', '2', '0'],
['1', '3', '2', '0']]
To sort this array you can use following code:
zip(*sorted(zip(*a), key=lambda column: column[0]))
where column[0] - value to be sorted by (you can use column1 etc.)
Output:
[('A', 'B', 'C', 'D'),
('0', '2', '3', '1'),
('0', '2', '3', '1')]
Note: If you are working with pretty big arrays and execution time does matter, consider using numpy, it has appropriate method: NumPy sort
How does your "2D array" look like?
For example:
>>> a = [
[12, 18, 6, 3],
[ 4, 3, 1, 2],
[15, 8, 9, 6]
]
>>> a.sort(key=lambda x: x[1])
>>> a
[[4, 3, 1, 2],
[15, 8, 9, 6],
[12, 18, 6, 3]]
But I guess you want something like this:
>>> a = [
[12, 18, 6, 3],
[ 4, 3, 1, 2],
[15, 8, 9, 6]
]
>>> a = zip(*a)
>>> a.sort(key=lambda x: x[1])
>>> a
[(6, 1, 9),
(3, 2, 6),
(18, 3, 8),
(12, 4, 15)]
>>> a = zip(*a)
>>> a
[(6, 3, 18, 12),
(1, 2, 3, 4),
(9, 6, 8, 15)
]
Python, per se, has no "2d array" -- it has (1d) lists as built-ins, and (1d) arrays in standard library module array. There are third-party libraries such as numpy which do provide Python-usable multi-dimensional arrays, but of course you'd be mentioning such third party libraries if you were using some of them, rather than just saying "in Python", right?-)
So I'll assume that by "2d array" you mean a list of lists, such as:
lol = [ range(10), range(2, 12), range(5, 15) ]
or the like -- i.e. a list with 3 items, each item being a list with 10 items, and the "second row" would be the sublist item lol[1]. Yeah, lots of assumptions, but your question is so maddeningly vague that there's no way to avoid making assumptions - edit your Q to clarify with more precision, and an example!, if you dislike people trying to read your mind (and probably failing) as you currently make it impossible to avoid.
So under these assumptions you can sort each of the 3 sublists in the order required to sort the second one, for example:
indices = range(10)
indices.sort(key = lol[1].__getitem__)
for i, sublist in enumerate(lol):
lol[i] = [sublist[j] for j in indices]
The general approach here is to sort the range of indices, then just use that appropriately sorted range to reorder all the sublists in play.
If you actually have a different problem, there will of course be different solutions;-).