I am having a lot of trouble sorting an array. The array contains 140 inner arrays of 3 points (x y z). I would like to sort all arrays based on lowest x coordinate to the highest. I have googled many methods and none of them have worked so far.

Basically I want to change:

[[1 2 3]
[8 9 7]
[4 5 6]
[5 4 3]]

into

[[1 2 3]
[4 5 6]
[5 4 3]
[8 9 7]]

But instead of 4.. there are 140 lines.

Any suggestions?

Version 2.4 of Python

## All 15 Replies

``````x= [[8, 9, 7],
[1, 2, 3],
[5, 4, 3],
[4, 5, 6]]

x.sort(key=lambda x: x[0])

print x``````
``````x= [[8, 9, 7],
[1, 2, 3],
[5, 4, 3],
[4, 5, 6]]

x.sort(key=lambda x: x[0])

print x``````

Every time I have tried that it gives me the error: key is an invalid keyword argument for this function

Is it not possible to upgrade to version 2.6?

``````x= [[8, 9, 7],
[1, 2, 3],
[5, 4, 3],
[4, 5, 6]]

x.sort(cmp=lambda x,y: cmp(x[0],y[0]))

print x``````

according this documetation the key should work:

The sort() method takes optional arguments for controlling the comparisons.

cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None.

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

Changed in version 2.3: Support for None as an equivalent to omitting cmp was added.

Changed in version 2.4: Support for key and reverse was added

cmp is an invalid keyword argument for this function

I agree. I read the same thing. The two suggestions you have supplied are what I have been trying. I do not know how to make it work since it is always coming back with error. The only time I have had anything work is when I use x.sort(0) in which it sorted all information from lowest to highest in all columns. Anything else has left the array exactly the same as how it was entered.

By default sort() sorts by the first item in each sublist ...

``````q = [[8, 9, 7], [1, 2, 3], [5, 4, 3], [4, 5, 6]]

q.sort()

print q  # [[1, 2, 3], [4, 5, 6], [5, 4, 3], [8, 9, 7]]``````

By default sort() sorts by the first item in each sublist ...

``````q = [[8, 9, 7], [1, 2, 3], [5, 4, 3], [4, 5, 6]]

q.sort()

print q  # [[1, 2, 3], [4, 5, 6], [5, 4, 3], [8, 9, 7]]``````

It doesn't seem to sort them in order. 24..22.20.28..14.14.14.20.. Is there any reason why this wouldn't work normally?

I installed version 2.4.4 just to test for you and it succeeded:

``````Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
>>> import os
>>> os.chdir('D:\\test')
>>> execfile('sortbyx.py')
[[1, 2, 3], [4, 5, 6], [5, 4, 3], [8, 9, 7]]
>>>``````

That's awesome... but, then why doesn't mine work?

Maybe you have redifined sort somehow?

``sorted(listofcordinates)``

?

Maybe you have redifined sort somehow?

``sorted(listofcordinates)``

?

Error: The truth value of an array with more than one value is ambiguous. Please use a.any() or a.all()

I noticed that everyone is putting commas. There are none in my array and i can't change that (predetermined data)... is there something different I have to do? I've noticed details often make a big difference...

Are you running the plain Python prompt?

Are you doing before testing the sort anything like

from xxx import *

Start Python command prompt and enter one test by hand. Or run my code directly by double clicking after adding raw_input('Ready') as last line

What says this when you run it:

``````import sys
if sys.version.startswith('2.4'):
print "You are running vesion 2.4"
else:
print "Not version 2.4"

print sys.version

Ah ha... hours later I figured out the problem. Thank you so much!!

While it was an array unfortuately it was collecting the coordinate points from a larger object (particle data).. so in the end I had to code in that script and arrange for the difference in sources.

tonyjv is right:

Your multiple arrays look a lot like numpy arrays.

``````import numpy as np

q = [[8, 9, 7], [1, 2, 3], [5, 4, 3], [4, 5, 6]]
print np.array(q)

"""my output -->
[[8 9 7]
[1 2 3]
[5 4 3]
[4 5 6]]
"""``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.