Python: Sort the numbers by the sum of their digits
Sort by Digit Sum
Write a Python program to sort the numbers in a given list by the sum of their digits.
Input: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Output: [10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19] Input: [23, 2, 9, 34, 8, 9, 10, 74] Output: [10, 2, 23, 34, 8, 9, 9, 74]
Visual Presentation:
 
Sample Solution-1:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
    # Sort the numbers in 'nums' based on the sum of their digits
    return sorted(nums, key=lambda n: sum(int(c) for c in str(n) if c != "-"))
# Assign a specific list of numbers 'nums' to the variable
nums = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# Print a message indicating the original list of numbers
print("Original list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Assign another specific list of numbers 'nums' to the variable
nums = [23, 2, 9, 34, 8, 9, 10, 74]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Sample Output:
Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Sort the numbers of the said list by the sum of their digits: [10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19] Original list of numbers: [23, 2, 9, 34, 8, 9, 10, 74] Sort the numbers of the said list by the sum of their digits: [10, 2, 23, 34, 8, 9, 9, 74]
Flowchart:
 
Sample Solution-2:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
    # Create a copy of the original list 'nums' to preserve the original order
    unordered = nums.copy()
    # Initialize an empty list to store the ordered numbers
    ordered = []
    
    # Continue the process until there are no more unordered numbers
    while unordered:
        # Select the first number in the unordered list as the smallest
        smallest = unordered[0]
        # Calculate the sum of digits for the current smallest number
        s = sum(int(c) for c in str(smallest) if c != "-")
        
        # Iterate through the remaining unordered numbers
        for t in unordered:
            # Calculate the sum of digits for the current number 't'
            t_s = sum(int(c) for c in str(t) if c != "-")
            
            # Compare the sum of digits and update the smallest if needed
            if t_s < s:
                smallest = t
                s = t_s
        
        # Append the smallest number to the ordered list
        ordered.append(smallest)
        # Remove the smallest number from the unordered list
        unordered.remove(smallest)
    
    # Return the final ordered list
    return ordered
# Assign a specific list of numbers 'nums' to the variable
nums = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# Print a message indicating the original list of numbers
print("Original list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Assign another specific list of numbers 'nums' to the variable
nums = [23, 2, 9, 34, 8, 9, 10, 74]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:", nums)
# Print a message indicating the operation to be performed
print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Sample Output:
Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Sort the numbers of the said list by the sum of their digits: [10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19] Original list of numbers: [23, 2, 9, 34, 8, 9, 10, 74] Sort the numbers of the said list by the sum of their digits: [10, 2, 23, 34, 8, 9, 9, 74]
Flowchart:
 
For more Practice: Solve these Related Problems:
- Write a Python program to sort a list of numbers based on the sum of their digits using a custom sort key.
- Write a Python program to compute the digit sum for each number and order the list in ascending order of these sums.
- Write a Python program to use lambda functions with sorted() to arrange numbers by their digit sum.
- Write a Python program to implement digit sum sorting without converting numbers to strings, using arithmetic instead.
Go to:
Previous: Find the largest integer divisor of a number n that is less than n.
Next: Determine which triples sum to zero.
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
