I was working with JavaScript and I needed an argsort() function that accepts a vector/array/list and returns the indices if the vector were to be sorted.
For example, if vec = [44.4, 99.9, 11.1, 33.3, 22.2] then argsort(vec) should return [2, 4, 3, 0, 1] because the smallest value in vec is 11.1 at [2], the second smallest value in vec is 22.2 at [4], and so on.
So, I did a quick Internet search and found a lengthy Stack Overflow discussion that presented several implementations of an argsort() function in JavaScript. However, all the examples given used exotic syntax such as lambda expressions, arrow functions, and method chaining with the dot operator.
I don’t like exotic syntax so I decided to write a simple JavaScript argsort() without using any exotic syntax:
function argsort(vec)
{
// vec is a numeric array
// suppose vec = [44.4, 99.9, 11.1, 33.3, 22.2]
let n = vec.length; // 5
let augmented = [];
for (let i = 0; i < n; ++i) {
augmented[i] = [vec[i], i]; // add indices to values
}
// augmented is [[44.4, 0], [99.9, 1] . . [22.2, 4]]
augmented.sort(function(a, b){return a[0] - b[0]});
// augmented is [[11.1, 2], [22.2, 4] . . [55.5, 1]]
let result = [];
for (let i = 0; i < n; ++i) {
result[i] = augmented[i][1]; // fetch the indexes
}
return result; // 2, 4, 3, 0, 1
}
The idea is to augment the source vector with indices 0, 1, 2, etc., then sort the augmented vector by the values (dragging along the indices), and then extract the sorted indices. The output of a short demo is:
Begin argsort() demo Source vector: [ 44.4, 99.9, 11.1, 33.3, 22.2 ] Result of argsort: [ 2, 4, 3, 0, 1 ]
I guess my point is that when I write software, I almost always prefer simplicity over cleverness and tiny improvements in speed.

Whenever I watch a movie, I always pay close attention to the opening and closing credit sequences. I especially enjoy animated movie title and closing sequences. I prefer the simplicity of animation to complex video sequences. Here’s an example from the wonderful animated movie “Monsters, Inc.” (2001).
Demo program. Replace “lt” with the less-than operator symbol (my blog editor chokes on symbols).
// test_argsort.js
function argsort(vec)
{
// vec is a numeric array
// suppose vec = [44.4, 99.9, 11.1, 33.3, 22.2]
let n = vec.length;
let augmented = [];
for (let i = 0; i "lt" n; ++i) {
augmented[i] = [vec[i], i];
}
// augmented is [44.4, 0], [99.9, 1] . . [22.2, 4]
augmented.sort(function(a, b){return a[0] - b[0]});
// augmented is [11.1, 2], [22.2, 4] . . [55.5, 1]
let result = [];
for (let i = 0; i "lt" n; ++i) {
result[i] = augmented[i][1]; // fetch the indexes
}
return result; // 2, 4, 3, 0, 1
}
console.log("\nBegin argsort() demo ");
let vec = [44.4, 99.9, 11.1, 33.3, 22.2];
// 0 1 2 3 4
// goal: 2, 4, 3, 0, 1
console.log("\nSource vector: ");
console.log(vec);
let idxs = argsort(vec);
console.log("\nResult of argsort: ");
console.log(idxs);

.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
2026 Visual Studio Live
2025 Summer MLADS Conference
2025 DevIntersection Conference
2025 Machine Learning Week
2025 Ai4 Conference
2025 G2E Conference
2025 iSC West Conference
You must be logged in to post a comment.