tags:

views:

163

answers:

1

I have a Multidimensional array in Python.

How do I go about sorting the second array, by the first - all the while keeping it in the same order?

+2  A: 

I'm not sure from your answer if this is what you want, but take a look and see. If I have a multidimensional array x:

>>> x = [[100,50,39,69,22,23,19,80,94,72],range(10)]
>>> print x
[[100, 50, 39, 69, 22, 23, 19, 80, 94, 72], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

and I want to sort the second subarray by the first subarray, I could do the following:

>>> x[1].sort(key = x[0].__getitem__)
>>> print x
[[100, 50, 39, 69, 22, 23, 19, 80, 94, 72], [6, 4, 5, 2, 1, 3, 9, 7, 8, 0]]

Is that what you're looking for?

Justin Peel
Can you explain what kind of order this is sorting against?
Xavier Ho
It's like the 0 in the second array is has a value of 100 attached to it that is its 'sort value or key' and 1 in the second array has 50 as its sort value and so forth (2 has sort value 39, 3 has sort value 69). I'm sorting the second array by the values of the first array. That was my guess as to what the OP was asking.
Justin Peel