Given an array **arr[]** of **N** non-negative integers, the task is to sort these integers according to sum of their digits.

**Examples:**

Input:arr[] = {12, 10, 102, 31, 15}

Output:10 12 102 31 15

10 => 1 + 0 = 1

12 => 1 + 2 = 3

102 => 1 + 0 + 2 = 3

31 => 3 + 1= 4

15 => 1 + 5 = 6

Input:arr[] = {14, 1101, 10, 35, 0}

Output:0 10 1101 14 35

**Approach:** The idea is to store each element with its sum of digits in a vector pair and then sort all the elements of the vector according to the digit sums stored. Finally, print the elements in order.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the sum ` `// of the digits of n ` `int` `sumOfDigit(` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` `while` `(n > 0) { ` ` ` `sum += n % 10; ` ` ` `n = n / 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Function to sort the array according to ` `// the sum of the digits of elements ` `void` `sortArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Vector to store digit sum with respective element ` ` ` `vector<pair<` `int` `, ` `int` `> > vp; ` ` ` ` ` `// Inserting digit sum with element in the vector pair ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `vp.push_back(make_pair(sumOfDigit(arr[i]), arr[i])); ` ` ` `} ` ` ` ` ` `// Quick sort the vector, this will sort the pair ` ` ` `// according to sum of the digits of elements ` ` ` `sort(vp.begin(), vp.end()); ` ` ` ` ` `// Print the sorted vector content ` ` ` `for` `(` `int` `i = 0; i < vp.size(); i++) ` ` ` `cout << vp[i].second << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 14, 1101, 10, 35, 0 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `sortArr(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the sum ` `# of the digits of n ` `def` `sumOfDigit(n) : ` ` ` ` ` `sum` `=` `0` `; ` ` ` `while` `(n > ` `0` `) : ` ` ` ` ` `sum` `+` `=` `n ` `%` `10` `; ` ` ` `n ` `=` `n ` `/` `/` `10` `; ` ` ` ` ` `return` `sum` `; ` ` ` `# Function to sort the array according to ` `# the sum of the digits of elements ` `def` `sortArr(arr, n) : ` ` ` ` ` `# Vector to store digit sum with ` ` ` `# respective element ` ` ` `vp ` `=` `[]; ` ` ` ` ` `# Inserting digit sum with element ` ` ` `# in the vector pair ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `vp.append([sumOfDigit(arr[i]), arr[i]]); ` ` ` ` ` `# Quick sort the vector, this will ` ` ` `# sort the pair according to sum ` ` ` `# of the digits of elements ` ` ` `vp.sort() ` ` ` ` ` `# Print the sorted vector content ` ` ` `for` `i ` `in` `range` `(` `len` `(vp)) : ` ` ` `print` `(vp[i][` `1` `], end ` `=` `" "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[` `14` `, ` `1101` `, ` `10` `, ` `35` `, ` `0` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` `sortArr(arr, n); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

0 10 1101 14 35

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort the numbers according to their product of digits
- Sort integers in array according to their distance from the element K
- Sort the array according to their cubes of each element
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Print numbers with digits 0 and 1 only such that their sum is N
- Count of groups having largest size while grouping according to sum of its digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Minimum digits to be removed to make either all digits or alternating digits same
- Sort ugly numbers in an array at their relative positions
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sort an array according to the order defined by another array
- Sort an array according to absolute difference with given value
- Sort an array of strings according to string lengths
- Sort an array according to absolute difference with a given value "using constant extra space"
- Python | Sort a List according to the Length of the Elements
- Python | Sort a list according to the second element in sublist
- Python | Sort an array according to absolute difference
- Sort the array of strings according to alphabetical order defined by another string
- Sort an array according to count of set bits | Set 2
- Sort an array according to absolute difference with given value using Functors

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.