How to sort the following matrix:
import numpy as np
A = np.array([['2', '2', '2', 'd'],
['1', '1', '3', 'c'],
['1', '13', '1', 'a'],
['1', '11', '3', 'b']], dtype='<U2')
based on the numbers in the text cells and based on multiple columns? With lists sorting works as follows:
sorted([[2, 2, 2, 'd'],
[1, 1, 3, 'c'],
[1, 13, 1, 'a'],
[1, 11, 3, 'b']], key = lambda k: (k[0],k[2],-k[1]))
Out[1]: [[1, 13, 1, 'a'], [1, 11, 3, 'b'], [1, 1, 3, 'c'], [2, 2, 2, 'd']]
but how could I sort the numpy array A similar to the list above?
First trial was not successful ...
sorted(A, key = lambda k: (k[0],k[2],-k[1]))
Traceback (most recent call last):
File "< ipython-input-... >", line 1, in sorted(A, key = lambda k: (k[0],k[2],-k[1]))
TypeError: bad operand type for unary -: 'numpy.str_'