1

There are two arrays: The first one contain numbers, and the second one contains "weight" of the first array values.

It works like this:

arr1 = [56,65,100,89,180,90];

"Weight" of the numbers are calculated in this way:

 56 = 5+6 = 11;
 65 = 6+5 = 11;
 100 = 1+0+0 = 1; and so on..

So, arr2 = [11,11,1,17,9,9];

My question is how can I sort the values of the arr1 according to values of arr2?

I tried to modify simple bubble sort for this problem, but nothing changed.

function bubble(arr1, arr2) {
  var len = arr1.length;
  for (var i = 0; i < len; i++) {
    for (var j = 0; j < len - i - 1; j++) {
      if (arr2[j] > arr2[j + 1]) {
        var temp = arr1[j];
        arr1[j] = arr1[j + 1];
        arr1[j + 1] = temp;
      }
    }
  }
  return arr1;
}

arr1 = [56, 65, 100, 89, 180, 90];

arr2 = [11, 11, 1, 17, 9, 9];

console.log(bubble(arr1, arr2));

I expect the output of the bubble function to be [100,180,90,56,65,89]. This is why:

FirstArray -          [56,65,100,89,180,90] - arr1
"Weight of the values"[11,11, 1, 17, 9, 9 ] - arr2
Output                [100,180,90,56,65,89]
                      [1,  9,  9 ,11,11,17]
4
  • 1
    Possible duplicate of Javascript - sort array based on another array Commented Aug 21, 2019 at 21:29
  • You have to sort both arrays, not just one. Otherwise the arrays start to missmatch and you get chaos Commented Aug 21, 2019 at 21:36
  • @JonasWilms if second array is derived from first , then sorting arr1 alone works - codepen.io/nagasai/pen/WNeoaEv?editors=1010 Commented Aug 21, 2019 at 21:42
  • Why not just supply a comparison function and compute the "weight" during the sort? Commented Aug 21, 2019 at 21:43

2 Answers 2

2

You can just calculate the weight on the fly while sorting:

const arr = [56,65,100,89,180,90];

arr.sort( (a,b) => 
  (a + '').split( '' ).reduce( (sum,x) => sum + +x, 0 ) -
  (b + '').split( '' ).reduce( (sum,x) => sum + +x, 0 )
);

console.log( arr );

Sign up to request clarification or add additional context in comments.

Comments

0

To achieve expected result, use below option of sorrting arr1 with logic mentioned in question

  1. Sort arr1 bycomparing sum of digits
  2. Convert number to string and split it to array
  3. Use reduce sum all the digits and compare with the array elements

var arr1 = [56, 65, 100, 89, 180, 90];
var arr2 = [11, 11, 1, 17, 9, 9];

console.log(
  arr1.sort(
    (a, b) =>
      a
        .toString()
        .split("")
        .reduce((acc, v) => parseInt(acc) + parseInt(v)) -
      b
        .toString()
        .split("")
        .reduce((acc, v) => parseInt(acc) + parseInt(v))
  )
);

codepen - https://codepen.io/nagasai/pen/WNeoaEv?editors=1010

1 Comment

My thanks! I just feel a little bit weak in arrow functions, so I need more practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.