0

When A is .sorted(), it becomes 6, 7, 8, so those number get new index values, so do the same for B. get current index of the values inside B, then rearrange it based on A’s new arrangement. So when A = 6,7,8, B would be u, z, h

var arrA = [8, 6, 7] // B follows this arr (A)
var arrB = ['h', 'u', 'z'] // B follows A

arrA.sort()
// output: 6, 7, 8

// arrB.followA’s arrangement somehow 
// output: u, z, h


arrA.reverse()
// output: 8, 7, 6

// arrB.follow A’s arrangement
// output: h, z, u


console.log(arrA);
console.log(arrB)
Ginfio
  • 57
  • 7
  • Does this question/answer help you? https://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array – Lemondoge Nov 29 '20 at 17:55

4 Answers4

0

Use a brute force sort routine. Swap elements in "ary", and if those swap, then swap the same indexes in "bry". This way the index swapping will mirror each other.

var ary = [8, 7, 4, 3, 5, 1, 6];
var bry = ['u', 'z', 'y', 'a', 'b', 'f', 'r'];
var i = 0;
var j = 0;

for (i = 0; i < ary.length-1; i++){
    for (j = 0; j < ary.length-1; j++){
      if (ary[j] > ary[j+1]){
          let bigger = ary[j];
          ary[j] = ary[j+1];
          ary[j+1] = bigger;

     // make bry swap the same indexes as ary above
          bigger = bry[j];
          bry[j] = bry[j+1];
          bry[j+1] = bigger;
      }
    }
}
emonz
  • 4,314
  • 1
  • 10
  • 4
0

It's not possible if it is two different arrays. the simple approach to solve ur problem is to convert your second array into an object.

let b = {
    8: 'H',
    6: 'U',
    7: 'Z'
}

arrB = arrA.map((val) => return b[val]);

and use this object to get the value for array B based on the values of array A.

correct me if I'm wrong

  • What if it is 2 different arrays? Is there a way to push the second array into the first one so it becomes one like you have it above. I have two different arrays: arrA [8, 6, 7], and arrB [h, u, z], now pus arrB to arrA so it becomes an object like you have jt above b = {8:h, ...} – Ginfio Nov 29 '20 at 21:11
  • arrA.forEach((key, index) => b[key] = arrB[index]); will map the arrays into object like I have above. – kishore Rajendran Nov 30 '20 at 05:35
0

Create a two-dimensional working array, whose elements are pairs of values from arrA and arrB:

var work = [];
arrA.forEach(function( v, i ) {
    work[ i ] = [ arrA[i], arrB[i] ];
});

Now you can arrange work in any order, and the values from arrA and arrB will stay in lockstep:

work.sort(function(x, y) {
    return Math.sign( x[0], y[0] );
});

(In the above example, we are ordering by the element in slot 0, which is from arrA. To order by the elements in arrB, change 0 to 1.)

Now you can alter work, e.g.:

work.reverse();

And extract the corresponding elements that were originally from arrA:

let newAarrA = work.map(function(x) {
    return x[0]; // from arrA
});
console.log(newArrA);

(Change 0 to 1 to get the corresponding elements from arrB instead.)

kmoser
  • 8,780
  • 3
  • 24
  • 40
  • How would we go about doing it with three arrays? – Ginfio Nov 30 '20 at 18:48
  • @Ginfio Change `work[ i ] = [ arrA[i], arrB[i] ];` to `work[ i ] = [ arrA[i], arrB[i], arrC[i] ];` to add the elements from `arrC` to the `work` array. For every element `elmt` in `work`, you can find the corresponding element from `arrA`, `arrB`, or `arrC` in `elmt[0]`, `elmt[1]`, and `elmt[2]` accordingly. Change the sort algorithm to use any of them in your sort criteria. – kmoser Nov 30 '20 at 21:20
0

You can use a "Sorting with map" technique, you basically use a temporary array that maps arrB's elements to values of arrA elements and then sort it.

var arrA = [8, 6, 7] // B follows this arr (A)
var arrB = ['h', 'u', 'z'] // B follows A

var tmpMap = arrB.map((el, i) => ({ index: i, value: arrA[i] }));

tmpMap.sort((a, b) => a.value - b.value);

var arrB = tmpMap.map(el => arrB[el.index]);

console.log(arrB);
Ivan Satsiuk
  • 333
  • 2
  • 9