[Solved] Sorting an array based on another array [closed]


You could use… but all depends on what you’re after – there are other ways…

>>> a = [5, 2, 3, 4, 1]
>>> b=  [3, 1, 5]
>>> sorted(b, key=a.index)
[5, 3, 1]

Or as @Manan has pointed out – you can in-place sort using a.sort(...)

solved Sorting an array based on another array [closed]