Drill

ProblemsPython › data

The order that sorts

harddataPython

A report lists items by rank without disturbing the source rows: instead of the values, it wants their positions.

sorted_index_order(values: list<int>) → list<int>

Solve it in the editor →

Where you start

def sorted_index_order(values: list[int]) -> list[int]:
    

Worked examples

CallResult
sorted_index_order([40, 10, 30, 10])[1, 3, 2, 0]
sorted_index_order([20, 10, 30, 10])[1, 3, 0, 2]
sorted_index_order([1, 2, 3])[0, 1, 2]
sorted_index_order([3, 2, 1])[2, 1, 0]

Hint

Sort a list of the indices with a comparator that looks the values up, falling back to the index itself on a tie.

Reference solution in Python
def sorted_index_order(values: list[int]) -> list[int]:
    idx = list(range(len(values)))
    idx.sort(key=lambda i: (values[i], i))
    return idx

The same problem in another language

More data problems in Python