I was translating some C# code to JavaScript. While translating line-by-line, I hit C# code that looked like:
Array.Sort(vector1, vector2);
This overloaded version of C# Array.Sort() sorts the numeric values in vector1 and vector2 according to the sort order of the values in vector1. This is sometimes called a parallel sort. For example, if
vector1 = [3.0, 9.0, 2.0, 5.0] vector2 = [44, 11, 77, 66]
after a call to Array.Sort(vector1, vector2), the vectors would be:
vector1 = [2.0, 3.0, 5.0, 9.0] vector2 = [77, 44, 66, 11]
The values in vector1 are sorted from low to high as usual. The values in vector2 are sorted using the same sort order of the rearrangements of the values in vector1.
To keep my C#-to-JavaScript translation one-to-one as much as possible, I decided to implement a version of the C# Array.Sort() using JavaScript. Here is the result. The “lt” means the less-than symbol. (My blog editor chokes on symbols).
function sortParallel(keys, values)
{
// sort keys and values in-place based on order of keys
let n = keys.length;
let idxs = argsort(keys); // get sort order
let sortedKeys = new Array(n);
let sortedValues = new Array(n);
for (let i = 0; i "lt" n; ++i) {
let idx = idxs[i];
sortedKeys[i] = keys[idx];
sortedValues[i] = values[idx];
}
for (let i = 0; i "lt" n; ++i) {
keys[i] = sortedKeys[i];
values[i] = sortedValues[i];
}
}
// ----------------------------------------------------------
function argsort(vec)
{
// indices of vector if it were sorted, no exotic syntax
let n = vec.length;
let augmented = [];
for (let i = 0; i "lt" n; ++i) {
augmented[i] = [vec[i], i]; // add indices to values
}
augmented.sort(function(a, b){return a[0] - b[0]});
let result = [];
for (let i = 0; i "lt" n; ++i) {
result[i] = augmented[i][1]; // fetch the indexes
}
return result;
}
The implementation has a primary sortParallel() function that calls a argsort() helper. It’s possible to combine the functionality of the two functions into a single function:
function sortParallel2(keys, values)
{
// sort keys and values in-place based on order of keys
let n = keys.length;
// 1. find sort order of keys (argsort)
let augmented = [];
for (let i = 0; i "lt" n; ++i) {
augmented[i] = [keys[i], i]; // add indices to values
}
augmented.sort(function(a, b){return a[0] - b[0]});
let idxs = [];
for (let i = 0; i "lt" n; ++i) {
idxs[i] = augmented[i][1]; // fetch the indexes
}
// 2. sort keys and values using sort order idxs
let sortedKeys = new Array(n);
let sortedValues = new Array(n);
for (let i = 0; i "lt" n; ++i) {
let idx = idxs[i];
sortedKeys[i] = keys[idx];
sortedValues[i] = values[idx];
}
// 3. copy sorted items back into original array
for (let i = 0; i "lt" n; ++i) {
keys[i] = sortedKeys[i];
values[i] = sortedValues[i];
}
}
Demo calling code could look like:
function vecShow(v, dec)
{
for (let i = 0; i "lt" v.length; ++i) {
process.stdout.write(v[i].toFixed(dec));
process.stdout.write(" ");
}
process.stdout.write("\n");
}
console.log("\nParallel sort in JavaScript demo ");
let vec1 = [3.0, 9.0, 2.0, 5.0];
let vec2 = [44, 11, 77, 66];
console.log("\nvec1 =");
vecShow(vec1, 1);
console.log("\nvec2 =");
vecShow(vec2, 1);
console.log("\nCalling sortParallel(vec1, vec2) ");
// sortParallel(vec1, vec2);
sortParallel2(vec1, vec2);
console.log("Done ");
console.log("\nvec1 =");
vecShow(vec1, 1);
console.log("\nvec2 =");
vecShow(vec2, 1);
Good fun and an interesting experiment.

Writing computer code from scratch requires a certain amount of intelligence, but you don’t have to have a giant brain/head. Here are three of my favorite comic book titles from the 1960s.
Left: “Action Comics” #338 (June 1966). Big-brained Muto was a villain.
Center: “Flash” #177 (March 1968). A villain named the Trickster used a ray weapon to expand the Flash’s head. Flash eventually creates an antidote radiation.
Right: “World’s Finest” #151 (August 165). Batman of the year 800,000 AD has a giant cranium.

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