What is the javascript equivalent of numpy argsort? What is the javascript equivalent of numpy argsort? numpy numpy

What is the javascript equivalent of numpy argsort?


You can use a Schwartzian transform also known as Decorate-Sort-Undecorate (DSU) in python.

DSU:

  1. Decorate - Use Array#Map to enrich each item in the array with the needed sort data
  2. Sort - sort using the added data
  3. Undecorate - extract the sorted data using Array#map again

Demo:

const dsu = (arr1, arr2) => arr1  .map((item, index) => [arr2[index], item]) // add the args to sort by  .sort(([arg1], [arg2]) => arg2 - arg1) // sort by the args  .map(([, item]) => item); // extract the sorted itemsconst clickCount = [5,2,4,3,1];const imgUrl = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'];const result = dsu(imgUrl, clickCount);  console.log(result);