
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sorting numbers based on their digit sums in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of positive integers, arr, as the first and the only argument.
Our function should sort the input array in such a way that the number that have the highest digit sum comes first followed by the numbers with lesser digit sums.
For example, if the input to the function is −
Input
const arr = [5, 34, 1, 13, 76, 8, 78, 101, 57, 565];
Output
const output = [565, 78, 76, 57, 8, 34, 5, 13, 101, 1];
Output Explanation
Because 565 have the highest digit sum of 16, followed by 78 and 76 and 101 and 1 have the least digit sum of 2 and 1 respectively
Example
Following is the code −
const arr = [5, 34, 1, 13, 76, 8, 78, 101, 57, 565]; const addDigits = (num, sum = 0) => { if(num){ return addDigits(Math.floor(num / 10), sum + (num % 10)); }; return sum; }; const sortByDigitSum = (arr = []) => { arr.sort((a, b) => { return addDigits(b) - addDigits(a); }); return arr; }; sortByDigitSum(arr); console.log(arr);
Output
[ 565, 78, 76, 57, 8, 34, 5, 13, 101, 1 ]
- Related Articles
- Sorting numbers according to the digit root JavaScript
- Sorting Array based on another array JavaScript
- Sorting array based on increasing frequency of elements in JavaScript
- Maximum and minimum sums from two numbers with digit replacements in C++
- Sorting string of words based on the number present in each word using JavaScript
- Program to sort numbers based on 1 count in their binary representation in Python
- Sorting the numbers from within in JavaScript
- Sorting according to weights of numbers in JavaScript
- Digit distance of two numbers - JavaScript
- Algorithm for sorting array of numbers into sets in JavaScript
- How do I change a tag's inner HTML based on their order in JavaScript?
- Finding nth digit of natural numbers sequence in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Reorder array based on condition in JavaScript?
- Queries on sum of odd number digit sums of all the factors of a number in C++

Advertisements