0

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.

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • Assuming you are using numpy: `foundFaces = foundFaces[foundFaces[:, 0].argsort()]`, check out https://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column – ewz93 Jul 24 '22 at 14:04
  • 1
    foundFaces = sorted(foundFaces, key=lambda x:x[0]) - or without numpy. – Hihikomori Jul 24 '22 at 14:08
  • @ewz93 Brilliant, I knew there'd be some numpy/pythonesc way of doing it. Thanks. Can you put it as answer? – Chud37 Jul 24 '22 at 16:55

2 Answers2

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

Assuming you are using NumPy:

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

Check out this thread.

ewz93
  • 2,444
  • 1
  • 4
  • 12