Drill

ProblemsJavaScript › data

The order that sorts

harddataJavaScript

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

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

Solve it in the editor →

Where you start

function sortedIndexOrder(values) {
  
}

Worked examples

CallResult
sortedIndexOrder([40,10,30,10])[1,3,2,0]
sortedIndexOrder([20,10,30,10])[1,3,0,2]
sortedIndexOrder([1,2,3])[0,1,2]
sortedIndexOrder([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 JavaScript
function sortedIndexOrder(values) {
  const idx = [];
  for (let i = 0; i < values.length; i++) idx.push(i);
  idx.sort((a, b) => values[a] - values[b] || a - b);
  return idx;
}

The same problem in another language

More data problems in JavaScript