Sorting numbers according to the digit root JavaScript

In this problem statement, our task is to sort numbers according to the digit root and implement this problem with the help of Javascript functionalities. So we can solve this problem with the help of loops in Javascript.

What is digit root?

The digit root of a given number is basically the sum of its digits. Repeat the calculation until the result is not a single digit. Let's take an example for the digit root, calculate the digit root for 1234, 1 + 2 + 3 + 4 = 10 = 1 + 0 = 1. Similarly the digit root of 456 is 6 because 4 + 5 + 6 = 15 = 1 + 5 = 6.

So digit roots are used basically in numerology and have different symbolic meanings. And they are also used in some mathematical algorithms, like the checksums used in credit card numbers, ISBNs and other codes.

Logic for the given problem

In the given problem statement we have to design a program to sort the given numbers as per their digit roots. If a number has a less digit root then put that number in ascending order. So for those numbers who have low digit root comes first in the sorted list.

For implementing this program we need to first calculate the digit roots of every number in the input array with the help of a for loop. Then we will sort the digit root array and the input array with the help of nested for loops and swap algorithms.


Step 1 − In this program, we need to create a function to sort the digit roots. And pass an argument in the function.

Step 2 − As we need to sort the numbers according to the digit roots, so to hold them we are required an array. So create a blank array.

Step 3 − Now with the help of a for loop we will check that the digit roots and the calculated roots are equal.

Step 4 − After declaring the above things, we will use nested loops to swap the array elements with the help of swap technique. And put the first number whose digit root is less than all the other numbers.

Step 5 − And the function's final step is to return the sorted array.

Step 6 − In the next step, we will write another function to calculate the digit root of every integer number in the array.

Step 7 − Now we will create a variable to keep track of numbers.

Step 8 − Next, we will use a while loop to check whether the given number is more than 9, and if the condition is true then compute the digit root of it.

Code for the algorithm

//function to sort the digit roots
function digitRootSort(arr) {
   const digitRoots = [];
   for (let i = 0; i < arr.length; i++) {
   digitRoots[i] = calculateDigitRoot(arr[i]);
   for (let i = 0; i < digitRoots.length; i++) {
      for (let j = i + 1; j < digitRoots.length; j++) {
         if (digitRoots[i] > digitRoots[j]) {
            const tempRoot = digitRoots[i];
            digitRoots[i] = digitRoots[j];
            digitRoots[j] = tempRoot;
            const tempNum = arr[i];
            arr[i] = arr[j];
            arr[j] = tempNum;
   return arr;
//function to calculate the digit root
function calculateDigitRoot(num) {
   let digitSum = num;
   while (digitSum > 9) {
      digitSum = digitSum.toString().split('').reduce((sum, digit) => sum + parseInt(digit), 0);
   return digitSum;
const arr = [13, 24, 32, 96, 102, 104, 98, 211, 998];


The above code has a time complexity of O(n²), where n is the size of the array. And we have used the nested loops for sorting. In the code every item in the array needs to be compared with the other item to find out their relative order based on their digit roots. And the space complexity is O(n), because it uses an array of size n to store the digit roots.


In the above code we have successfully developed a code to sort the numbers based on their digit roots. The code uses two functions to sort the numbers. First function determines the sorting process and the second function calculates the digit roots of every element of the array.

Updated on: 18-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started