Skip to main content
edited tags
Link
jpp
  • 165.5k
  • 37
  • 301
  • 362
added 138 characters in body
Source Link

I have a structured numpy array:

dtype = [('price', float), ('counter', int)]
values = [(35, 1), (36, 2),
          (36, 3)]
a = np.array(values, dtype=dtype)

I want to sort for price and then for counter if price is equal:

a_sorted = np.sort(a, order=['price', 'counter'])[::-1]

I need the price in a descending order and when prices are equal consider counter in ASCENDING order. In the example above both the price and the counter are in descending order.

What I get is:

a_sorted: [(36., 3), (36., 2), (35., 1)]

what I need is:

 a_sorted: [(36., 2), (36., 3), (35., 1)]

I have a structured numpy array:

dtype = [('price', float), ('counter', int)]
values = [(35, 1), (36, 2),
          (36, 3)]
a = np.array(values, dtype=dtype)

I want to sort for price and then for counter if price is equal:

a_sorted = np.sort(a, order=['price', 'counter'])[::-1]

I need the price in a descending order and when prices are equal consider counter in ASCENDING order. In the example above both the price and the counter are in descending order.

I have a structured numpy array:

dtype = [('price', float), ('counter', int)]
values = [(35, 1), (36, 2),
          (36, 3)]
a = np.array(values, dtype=dtype)

I want to sort for price and then for counter if price is equal:

a_sorted = np.sort(a, order=['price', 'counter'])[::-1]

I need the price in a descending order and when prices are equal consider counter in ASCENDING order. In the example above both the price and the counter are in descending order.

What I get is:

a_sorted: [(36., 3), (36., 2), (35., 1)]

what I need is:

 a_sorted: [(36., 2), (36., 3), (35., 1)]
Source Link

Sort Structured Numpy Array On Multiple Columns In Different Order

I have a structured numpy array:

dtype = [('price', float), ('counter', int)]
values = [(35, 1), (36, 2),
          (36, 3)]
a = np.array(values, dtype=dtype)

I want to sort for price and then for counter if price is equal:

a_sorted = np.sort(a, order=['price', 'counter'])[::-1]

I need the price in a descending order and when prices are equal consider counter in ASCENDING order. In the example above both the price and the counter are in descending order.