How can I sort 2d array includes only string characters in Python?

Question:

There is an array as below;

x=np.array([ ['0', '0'],
             ['1', '1'],
             ['7', '10'],
             ['8', '11'],
             [',', '2'],
             ['4', '3'],
             ['.', '4'],
             ['2', '5'],
             ['5', '6'],
             ['er014', '7'],
             ['ww', '8'],
             ['*', '9']])

I used the following codes to sort this array by the second column but without success. Is there anyone to help?

A= np.take(x, x[:, 1].argsort(), 0) 

Asked By: kyazgan

||

Answers:

np.take(x, x[:, 1].astype(int).argsort(), 0)
You may just cast the values for sorting. The overall result of you np.take() will remain as strings.

array([['0', '0'],
   ['1', '1'],
   [',', '2'],
   ['4', '3'],
   ['.', '4'],
   ['2', '5'],
   ['5', '6'],
   ['er014', '7'],
   ['ww', '8'],
   ['*', '9'],
   ['7', '10'],
   ['8', '11']], dtype='<U5')
Answered By: DiMithras
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.