0

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
  • can you provide an example? what do you mean sorting the second by the first and keeping the same order? Commented May 6, 2010 at 14:01
  • I'm unclear exactly how you want your array(s) sorted. Can you edit your question to provide a simple before/after example of what your array looks like before and after being sorted? Commented May 6, 2010 at 14:02

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain what kind of order this is sorting against?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.