I’m having a problem sorting a numpy array that has numbers as strings. I need to keep these as strings because there are other words after the integers.
It’s sorting negative numbers in reverse order:
>>> import numpy as np
>>> a = np.array(["3", "-2", "-1", "0", "2"])
>>> a.sort()
>>> a
array(['-1', '-2', '0', '2', '3'], dtype='|S2')
I would have expected the output to be:
array(['-2', '-1', '0', '2', '3'], dtype='|S2')
Any suggestions?
You could use natural sorting: