If you have control over the creation of the array, you could create a structured array instead of a regular array.
dtypes = [('value', np.float64), ('label', '<U32')]
a = np.array( [( 3e-05, 'A' ),
( 2, 'B' ),
( 1e-05, 'C' )], dtype=dtypes)
Now, a
is a structured array with separate dtypes for the first and second columns -- the first column is an array of floats, and the second column is an array of strings.
Note that the array is defined as a list of tuples. This is important: defining it as a list of lists and then specifying dtype=dtypes
won't work.
Now, you can sort by a column like so:
a_sorted = np.sort(a, order=['value'])
which gives:
array([(1.e-05, 'C'), (3.e-05, 'A'), (2.e+00, 'B')],
dtype=[('value', '<f8'), ('label', '<U32')])
You can get a row or column of this structured array like so:
>>> a_sorted[0]
(1.e-05, 'C')
>>> a_sorted['value']
array([1.e-05, 3.e-05, 2.e+00])