Python - Descending Sort String Numbers
Last Updated :
09 Apr, 2023
Reverse Sorting a list is easy task and has been dealt with in many situations. With Machine Learning and Data Science emerging, sometimes we can get the data in the format of list of numbers but with string as data type. Generic Sort functions give erroneous result in that case, hence several other methods have to employed to perform these particular task. Lets discuss ways in which this is performed.
Method #1 : Naive Method In the naive method requires the type conversion of all the elements into integers of the list iterated through a loop. After that generic sort function is employed to perform the task. The descending sorting is done by passing reverse.
Python3
# Python3 code to demonstrate
# Descending Sort String Numbers
# using naive method
# initializing list
test_list = [ '4', '6', '7', '2', '1']
# printing original list
print ("The original list is : " + str(test_list))
# Descending Sort String Numbers
# numeric string sorting
for i in range(0, len(test_list)) :
test_list[i] = int(test_list[i])
test_list.sort(reverse = True)
# printing result
print ("The resultant reverse sorted list : " + str(test_list))
Output : The original list is : ['4', '6', '7', '2', '1']
The resultant reverse sorted list : [7, 6, 4, 2, 1]
Time complexity: O(nlogn), where n is the length of the test_list.
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using sort() using key + reverse The generic sort() can be used to perform this particular task, but has to be specified with the key as integer to convert it to integer while performing sort function internally. The descending sorting is done by passing reverse.
Python3
# Python3 code to demonstrate
# Descending Sort String Numbers
# using sort() + key
# initializing list
test_list = [ '4', '6', '7', '2', '1']
# printing original list
print ("The original list is : " + str(test_list))
# using sort() + key
# Descending Sort String Numbers
test_list.sort(key = int, reverse = True)
# printing result
print ("The resultant reverse sorted list : " + str(test_list))
Output : The original list is : ['4', '6', '7', '2', '1']
The resultant reverse sorted list : ['7', '6', '4', '2', '1']
Time Complexity: O(n*nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Similar Reads
Python - Differential Sort String Numbers and Alphabets Given a List String, Reorder List, with Sorted Alphabets followed by Sorted Strings. Input : test_list = ["1", "G", "10", "L", "9", "K", "4"] Output : ['G', 'K', 'L', '1', '4', '9', '10'] Explanation : Alphabets sorted, succeeded by sorted digits. Input : test_list = ["1", "G", "10", "L", "9"] Outpu
5 min read
Sort Numeric Strings in a List - Python We are given a list of numeric strings and our task is to sort the list based on their numeric values rather than their lexicographical order. For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30.
2 min read
Python | Sort Numerical Records in String Sometimes, while working with Python records we can have a problem that they may occur in name and number format in strings. These may be required to be sorted. This problem can occur in many domains in which data is involved. Let us discuss certain ways in which this task can be performed. Method #
6 min read
Python - Sort Strings by Case difference Given Strings List, the task is to write a Python program to perform sort on basis of difference of cases i.e count of lower case and upper case. Examples: Input : test_list = ["GFG", "GeeKs", "best", "FOr", "alL", "GEEKS"] Output : ['GeeKs', 'FOr', 'alL', 'GFG', 'best', 'GEEKS'] Explanation : ees(3
6 min read
Sort Dictionary by Value Python Descending Sometimes, we need to sort the dictionary by value in reverse order or descending order. We can perform this task easily by using Python. In this article, we will learn to Sort the Dictionary by Value in Descending Order using Python Programming. Below is an example to understand the problem stateme
4 min read