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 Moinuddin Quadri for How do I sort a list with positives coming before negatives with values sorted respectively?

$
0
0

Create two lists, one with positive value and another with the negative values and then sort the content of each list the way you like. For example:

my_list = [1, -2, 10, -12, -4, -5, 9, 2]pos_list, neg_list = [], []for item in my_list:    if item < 0:         neg_list.append(item)    else:        pos_list.append(item)final_list = sorted(pos_list) + sorted(neg_list)

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>