Python | Sort numeric strings in a list

Sorting list is an 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 this particular task. Let’s 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# numeric string sorting
# using naive method 
  
# initializing list 
test_list = [ '4', '6', '7', '2', '1']
  
# printing original list 
print ("The original list is : " + str(test_list))
  
# using naive method 
# numeric string sorting
for i in range(0, len(test_list)) :
    test_list[i] = int(test_list[i])
test_list.sort()
  
# printing result
print ("The resultant sorted list  : " +  str(test_list))

chevron_right


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : [1, 2, 4, 6, 7]

 
Method #2 : Using sort() using key
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# numeric string sorting
# 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
# numeric string sorting
test_list.sort(key = int)
  
# printing result
print ("The resultant sorted list  : " +  str(test_list))

chevron_right


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

 
Method #3 : Using sorted() + key
This function has a similar internal working as the above function. The improvement that this function offers than the above function is that it doesn’t change the order of original list and just returns a view to display, hence useful at situations where order has to be maintained.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# numeric string sorting
# using sorted() + key
  
# initializing list 
test_list = [ '4', '6', '7', '2', '1']
  
# printing original list 
print ("The original list is : " + str(test_list))
  
# using sorted() + key
# numeric string sorting
res = sorted(test_list, key = int)
  
# printing result
print ("The resultant sorted list  : " +  str(res))

chevron_right


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.