Quantcast
Channel: How do I sort a list with positives coming before negatives with values sorted respectively? - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by John Smith for How do I sort a list with positives coming before negatives with values sorted respectively?

$
0
0

Kudos to wmin for the logic of his solution (accepted answer) which is great. So for completeness, a similar answer to this but based on numpy, which is significantly faster for anything else other than small lists/arrays, is as follows:

lst = [1, -2, 10, -12, -4, -5, 9, 2]ar = np.array(lst)ar.sort()i = ar.__abs__().argmin()np.concatenate((ar[i:], ar[:i]))

Viewing all articles
Browse latest Browse all 12

Trending Articles