Python program to sort digits of a number in ascending order Last Updated : 07 Jan, 2024 Summarize Comments Improve Suggest changes Share 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 Sort a list of strings in lexicographical order in Python V vikkycirus Follow Improve Article Tags : DSA number-digits Similar Reads Python - Sort words of sentence in ascending order Sorting words in a sentence in ascending order can be useful for tasks like text analysis, data preprocessing, or even fun applications like creating word puzzles. Itâs simple to achieve this using Python. In this article, we will explore different methods to do this.Using sorted() with SplitThe mos 3 min read Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat 2 min read Python program to Sort a list according to the second element of sublist Sorting a list by the second element of each sublist means rearranging the sublists based on the value at index 1. For example, given the list [[1, 3], [2, 1], [4, 2], [3, 5]], sorting by the second element results in [[2, 1], [4, 2], [1, 3], [3, 5]], where the second elements (1, 2, 3, 5) are in as 3 min read Python | Sort Tuples in Increasing Order by any key Given a tuple, sort the list of tuples in increasing order by any key in tuple. Examples: Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)] m = 0 Output : [(1, 2), (2, 3), (2, 5), (4, 4)] Explanation: Sorted using the 0th index key. Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)] m = 2 Output : So 3 min read Smallest number by rearranging digits of a given number Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of a given number. Examples: Input: n = 846903Output: 304689Input: n = 55010Output: 10055Input: n = -40505Output: -55400Steps to find the smallest number. Count the frequency of each digit in the number.If i 7 min read Sort an array in increasing order of their Multiplicative Persistence Given an array arr[] consisting of N positive integers, the task is to sort the array in increasing order with respect to the count of steps required to obtain a single-digit number by multiplying its digits recursively for each array element. If any two numbers have the same count of steps, then pr 8 min read Like