Sorts an array and returns the original indexes in sorted order.

Usage no npm install needed!

<script type="module">
  import sortIndexes from '';



Sorts the elements of an array and returns a new array with the indexes of the elements from the original array in sorted order.


sortIndexes(array[, comparator])


  • array the array to sort.
  • comparator (optional) function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.
    • firstEl The first element for comparison.
    • secondEl The second element for comparison.

Return value

A new array containing the indexes of array in the sorted order.


import sortIndexes from 'sort-indexes';

sortIndexes(['c', 'a', 'b']); // -> [2, 0, 1]

sortIndexes([5, 1, 3, 4, 2], (a, b) => b - a); // -> [0, 3, 2, 4, 1]