JSFiddle - React, Tailwind, and code Playground

JavaScript

function sortWithIndices(toSort) {
  for (var i = 0; i < toSort.length; i++) {
    toSort[i] = [toSort[i], i];
  }
  toSort.sort(function(left, right) {
    return left[0] < right[0] ? -1 : 1;
  });
  toSort.sortIndices = [];
  for (var j = 0; j < toSort.length; j++) {
    toSort.sortIndices.push(toSort[j][1]);
    toSort[j] = toSort[j][0];
  }
  return toSort;
}


var array1 = ['zzzz', 'aaaa', 'cccc'];
var array2 = [3, 7, 1];

// calculate the indices of array1 after sorting. (attached to array1.sortIndices)
sortWithIndices(array1);

// the final array after applying the sorted indices from array1 to array2
var final = [];

// apply sorted indices to array2
for(var i = 0; i < array1.sortIndices.length; i++)
    final[i] = array2[array1.sortIndices[i]];

// output results
alert(final.join(","));