Sorting according to weights of numbers in JavaScript


In this problem statement, our target is to sort the given array of numbers according to the weights of numbers with the help of Javascript. At the beginning we will write a function that calculates the weights of the numbers and then sort them with another function.

Understanding the Problem

We have to sort the provided numbers according to their weights. As a result we will have an array of numbers. A weight of the number is defined as the sum of its own digits. For example: suppose we have an array like [19, 11, 12, 15]. We need to sort these numbers on their weights. The weights of 19, 11, 12 and 15 are 10, 2, 3 and 6 respectively.

After sorting the numbers based on weights the resultant array should be [11, 12, 15, 19].

Logic for the given Problem

Define a function in the code to compute the weights of the given numbers. A number's weight is the sum of its own digits.

And construct another function to sort the numbers depending on their weights and will take an array of integers as an input. If the two numbers have the same weight then we will sort them as per their numerical value. In the implementation we will use the array sort method and pass a comparison function as an input. The weights of two numbers will be compared by the function we will pass within the sort method.

Algorithm

Step 1: Compute the weights of the given numbers with the help of user defined function.

Step 2: Create a function to sort the numbers by weight calculated by the first function. So this function will accept an argument of an array and sort these numbers with respect to their weights.

Step 3: Use the sort method to sort the numbers present in the array and also pass an argument of the comparison function.

Step 4: With the usage of weight function which we have defined in step 1. We will compare the weights of two numbers inside the comparison function wwe have passed in the sort method.

Step 5: If the weights of the numbers are the same or equal then we will compare their numerical values.

Step 6: We will return the sorted array with respect to the weights of the given digits.

Example

//Get the weights of numbers
function getWeight(num) {
   let sum = 0;
   while (num > 0) {
      sum += num% 10;
      num = Math.floor(num / 10);
   }
   return sum;
}

//Sort the numbers with respect to the weights
function sortByWeight(nums) {
   nums.sort((a, b) => {
      const wA = getWeight(a);
      const wB = getWeight(b);
      if (wA === wB) {
         return a - b;
      }
       return wA - wB;
   });
   return nums;
}

const nums = [123, 56, 12, 478, 99];
console.log(sortByWeight(nums));

Output

[ 12, 123, 56, 99, 478 ]

Complexity

The function's time complexity is O(n * m * log n), in which n is the size of the input array entries and m is the average number of digits in each number. Because for every number we have to calculate the corresponding weights which takes O(m) time. And then the sort function has a time complexity of O(n * log n).

Conclusion

So we have created a function to sort the given numbers based on their weights. And we have also handled the numbers having the same weight and arranged them based on their numerical values. But the concern is if the digit size will increase then the time complexity will also increase. So it is efficient for small size digits.

Updated on: 16-Aug-2023

811 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements