Python program to sort digits of a number in ascending order Last Updated : 07 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to sort the digits in ascending order. Print the new number obtained after excluding leading zeroes. Examples: Input: N = 193202042Output: 1222349Explanation: Sorting all digits of the given number generates 001222349.Final number obtained after removal of leading 0s is 1222349. Input: N = 78291342023Output:1222334789 Approach: Follow the steps below to solve the problem: Convert the given integer to its equivalent stringSort the characters of the string using join() and sorted().Convert string to integer using type castingPrint the integer obtained.Below is the implementation of the above approach: Python3 # Python program to # implement the above approach # Function to sort the digits # present in the number n def getSortedNumber(n): # Convert to equivalent string number = str(n) # Sort the string number = ''.join(sorted(number)) # Convert to equivalent integer number = int(number) # Return the integer return number # Driver Code n = 193202042 print(getSortedNumber(n)) Output1222349 Time Complexity: O(N*log(N))Auxiliary Space: O(N) Method#2: Using numpy: Algorithm : Initialize the input number nConvert the number to a string representation using str(n)Convert the string digits to a list of integers using list comprehension [int(x) for x in str(n)]Sort the digits list using numpy sort method np.sort(digits)Join the sorted digits as string using ''.join(map(str, np.sort(digits)))Convert the sorted digits string back to an integer using int()Return the sorted integerPrint the returned sorted integer Python3 import numpy as np def getSortedNumber(n): digits = [int(x) for x in str(n)] number = int(''.join(map(str, np.sort(digits)))) return number n = 193202042 print(getSortedNumber(n)) #This code is contributed by Jyothi pinjala. Output: 1222349 The time complexity : O(n log n), where n is the number of digits in the input number, because the np.sort() function uses a quicksort algorithm, which has an average time complexity of O(n log n). The auxiliary space : O(n), because we create a list of length n to hold the individual digits of the input number. Additionally, we create a string of length n to hold the sorted digits, and an integer variable to hold the final output. These variables are all constant in size with respect to the input, so they do not contribute to the space complexity. Comment More infoAdvertise with us Next Article Python program to sort digits of a number in ascending order vikkycirus Follow Improve Article Tags : Sorting Mathematical Technical Scripter Competitive Programming Python Programs DSA number-digits +3 More Practice Tags : MathematicalSorting Similar Reads Python program to find smallest number in a list In this article, we will discuss various methods to find smallest number in a list. The simplest way to find the smallest number in a list is by using Python's built-in min() function.Using min()The min() function takes an iterable (like a list, typle etc.) and returns the smallest value.Pythona = [ 2 min read Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number 3 min read Sorting List of Dictionaries in Descending Order in Python The task of sorting a list of dictionaries in descending order involves organizing the dictionaries based on a specific key in reverse order. For example, given a list of dictionaries like a = [{'class': '5', 'section': 3}, {'Class': 'Five', 'section': 7}, {'Class': 'Five', 'section': 2}], the goal 3 min read Python program to find the smallest number in a file Given a text file, write a Python program to find the smallest number in the given text file.Examples:Input: gfg.txtOutput: 9Explanation: Contents of gfg.txt: I live at 624 Hyderabad.My mobile number is 52367. My favourite number is 9.Numbers present in the text file are 9,624,52367Minimum number is 3 min read Sum the Digits of a Given Number - Python The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. Using modulo (%)This method efficiently extracts each digit using the modulus (%) and integer di 2 min read Python - Sort list of numbers by sum of their digits Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case.Using sorted() with a Lambda Functionsorted() function w 2 min read Python program to list Sort by Number value in String Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4 6 min read Python program to print sorted number formed by merging all elements in array Given an array arr[], the task is to combine all the elements in the array sequentially and sort the digits of this number in ascending order. Note: Ignore leading zeros. Examples: Input: arr =[7, 845, 69, 60]Output: 4566789Explanation: The number formed by combining all the elements is "78456960" a 4 min read Python program to insert an element into sorted list Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function 2 min read Python Program to move numbers to the end of the string Given a string, the task is to write a Python program to move all the numbers in it to its end. Examples: Input : test_str = 'geek2eeks4g1eek5sbest6forall9' Output : geekeeksgeeksbestforall241569 Explanation : All numbers are moved to end. Input : test_str = 'geekeeksg1eek5sbest6forall9' Output : ge 6 min read Like