Sometimes, while working with Python, we can have a problem in which we need to sort a particular list which has mixed data types. That it contains integers and strings and we need to sort each of them accordingly. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using
sort() + comparator
This problem can be solved using sort functionality provided by Python. We can construct our own custom comparator to complete the task of mixed sort.
The original list : [4, 'gfg', 2, 'best', 'is', 3] List after mixed sorting : [2, 3, 4, 'best', 'gfg', 'is']
Method #2 : Using
sorted() + key + lambda + isdigit()
The combination of above functionalities can also be used to achieve solution to this problem. In this, we just sort the list using sorted() using key functionality using lambda function to segregate digits using isdigit().
The original list : ['4', 'gfg', '2', 'best', 'is', '3'] List after mixed sorting : ['2', '3', '4', 'best', 'gfg', 'is']
- Python - Numeric Sort in Mixed Pair String List
- Python | Convert mixed data types tuple list to string list
- Python | Integer count in Mixed List
- Python - Type conversion in Nested and Mixed List
- Python | Lexicographically smallest string in mixed list
- Python | Count String occurrences in mixed list
- Python | Find Mixed Combinations of string and list
- Python | Multiply Integer in Mixed List of string and numbers
- Python | Convert numeric String to integers in mixed List
- Python - Maximum column values in mixed length 2D List
- Python - Convert delimiter seperated Mixed String to valid List
- Python - Sort dictionaries list by Key's Value list index
- Python | Sort list according to other list order
- Python | Sort list of list by specified index
- Python | Sort the values of first list using second list
- Python | Sort Flatten list of list
- Python list sort()
- Python - Sort from Kth index in List
- Python | How to sort a list of strings
- Python | Sort a list of percentage
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.