-1

I would like to sort each integer value in ascending order and have each P value move along with it.

Input:

[['P1', 168], ['P2', 146], ['P3', 116], ['P4', 113], ['P5', 113], ['P6', 90], ['P7', 28]]

Output:

[['P7', 28],['P6', 90],['P4', 113], ['P5', 113],['P3', 116], ['P2', 146], ['P1', 168]]  
Jarod
  • 51
  • 5
  • 1
    What have your tried so far? Show us your work and tell us where you are stuck. Stackoverflow isn't for getting your homework answers – whiplash Oct 24 '22 at 17:02
  • Sorry, second time posting ever. Was not aware of how specific I should be with what I have. As this is now marked as a duplicate I believe changing it is redundant. But thank you for providing me with more insight into how I might better my future posts. – Jarod Oct 24 '22 at 19:02

1 Answers1

0

For

a=[['P1', 168], ['P2', 146], ['P3', 116], ['P4', 113], ['P5', 113], ['P6', 90], ['P7', 28]]

One solution could be:

import numpy as np

a1 = np.sort(a, axis=-1, kind=None, order=None)
a1[:,[0,1]] = a1[:,[1,0]][::-1] <=========== [::-1] to reverse the order
a1

Output:

enter image description here

learner
  • 603
  • 1
  • 3
  • 15