Sorting 2D by rank

Asked by At

I want to sort my 2D array to match the rank of another array

I have tried the sorted() and sort() function and it's not working. I am not sure if I am formatting it wrong or what is the best way to do it.

array = (32,96)
ranked =  [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67,
       44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51,
       92, 86, 69, 47,  0,  7,  3, 85, 18, 11, 13, 53,  8, 78, 82, 81, 14,
       74, 59, 32, 42, 39,  1, 31, 36, 19, 24,  5, 38,  9, 73, 71, 76, 87,
       41, 55, 94, 93, 84, 16, 90, 62, 48, 43, 72, 95, 65, 45, 61, 22, 21,
       15, 37, 88,  2, 40, 56,  6, 12, 60,  4, 35]
sortedarray = sorted(array,ranked)

for the code above i get an error:

TypeError: 'list' object is not callable

ranked has 96 values that correspond to the second dimension in the original array.

1 Answers

1
gabriel.ibagon On Best Solutions

To sort the second dimension of array, you can first make an empty sortedarray of shape (32, 96) and then fill in the new array in the ordered specified by ranked. Here is the code using random values for array:

array = np.random.random((32,96))
ranked =  [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67,
       44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51,
       92, 86, 69, 47,  0,  7,  3, 85, 18, 11, 13, 53,  8, 78, 82, 81, 14,
       74, 59, 32, 42, 39,  1, 31, 36, 19, 24,  5, 38,  9, 73, 71, 76, 87,
       41, 55, 94, 93, 84, 16, 90, 62, 48, 43, 72, 95, 65, 45, 61, 22, 21,
       15, 37, 88,  2, 40, 56,  6, 12, 60,  4, 35]

sortedarray = np.zeros(np.shape(array))
sortedarray[:, ranked] = array