Digital root sort algorithm JavaScript

Digital root of a positive integer is defined as the sum of all of its digits. We are given an array of integers and need to sort it based on digit root values. If a number comes before b, then the digit root of a should be less than or equal to the digit root of b. If two numbers have the same digit root, the smaller number should come first.

For example, 4 and 13 have the same digit root (4 and 1+3=4), but since 4 < 13, the number 4 comes before 13 in the sorted array.

Example Input and Output

Input: [13, 20, 7, 4]
Output: [20, 4, 13, 7]

Let's break down why this sorting works:

  • 20 has digit root: 2+0 = 2
  • 4 has digit root: 4
  • 13 has digit root: 1+3 = 4
  • 7 has digit root: 7

Sorted by digit root: 20(2), then 4(4) and 13(4) where 4 comes before 13, then 7(7).

Implementation

We'll create two functions: a recursive function to calculate the sum of digits, and a sorting function that orders elements based on digit root.

const arr = [54, 23, 8, 89, 26];

// Recursive function to calculate digit root (sum of all digits)
const recursiveCount = (num, count = 0) => {
    if(num){
        return recursiveCount(Math.floor(num/10), count + num % 10);
    }
    return count;
};

// Sorting function based on digit root
const sorter = (a, b) => {
    const countDifference = recursiveCount(a) - recursiveCount(b);
    return countDifference || a - b; // If digit roots are equal, sort by value
};

arr.sort(sorter);
console.log(arr);
[ 23, 8, 26, 54, 89 ]

How It Works

The recursiveCount function calculates the digit root by:

  1. Taking the last digit using num % 10
  2. Adding it to the running count
  3. Recursively calling itself with Math.floor(num/10) to remove the last digit
  4. Returning the total count when no digits remain

The sorter function compares two numbers by their digit roots first, and if they're equal, it compares the actual values.

Step-by-Step Breakdown

// Let's trace through the digit root calculation
console.log("Digit roots:");
console.log("54 ->", recursiveCount(54)); // 5+4 = 9
console.log("23 ->", recursiveCount(23)); // 2+3 = 5
console.log("8 ->", recursiveCount(8));   // 8
console.log("89 ->", recursiveCount(89)); // 8+9 = 17
console.log("26 ->", recursiveCount(26)); // 2+6 = 8
Digit roots:
54 -> 9
23 -> 5
8 -> 8
89 -> 17
26 -> 8

Alternative Iterative Approach

const iterativeDigitRoot = (num) => {
    let sum = 0;
    while (num > 0) {
        sum += num % 10;
        num = Math.floor(num / 10);
    }
    return sum;
};

const arr2 = [54, 23, 8, 89, 26];
arr2.sort((a, b) => {
    const rootA = iterativeDigitRoot(a);
    const rootB = iterativeDigitRoot(b);
    return rootA - rootB || a - b;
});

console.log(arr2);
[ 23, 8, 26, 54, 89 ]

Conclusion

Digital root sorting arranges numbers primarily by the sum of their digits, with original values as tiebreakers. This algorithm combines custom comparison logic with JavaScript's built-in sort method for efficient implementation.

Updated on: 2026-03-15T23:18:59+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements