Chud37
Chud37

Reputation: 5017

Sort array by first column

I have an array called foundFaces. However, I want to order the faces from left to right, so that its easy to guess which face is which.

When I print the array I get this:

[[414.7081      92.99516    482.82443    177.7601       0.9049758 ]
 [278.0241      96.27542    344.90128    186.57086      0.88056326]
 [542.65515     69.08964    609.4397     162.48027      0.88008916]]

The left most column is the x1 value, which I would like to sort by. So the final array I want is this:

[[278.0241      96.27542    344.90128    186.57086      0.88056326]
 [414.7081      92.99516    482.82443    177.7601       0.9049758 ]
 [542.65515     69.08964    609.4397     162.48027      0.88008916]]

How can I achieve this in python? Do I have to loop through and create my own array or is there a fancy way to do it? I'm still new to python so any help is appreciated.

Upvotes: 0

Views: 779

Answers (2)

EliasK93
EliasK93

Reputation: 3184

Assuming you are using NumPy:

foundFaces = foundFaces[foundFaces[:, 0].argsort()]

Check out this thread.

Upvotes: 0

AveragePythonEnjoyer
AveragePythonEnjoyer

Reputation: 1165

sorted(foundFaces, key=lambda x: x[0])

Upvotes: 1

Related Questions