Sort an array by value using Python
Question:
Write a program that sorts an array by its data value python, the output will be like this
Here is my code so far
iniArray = ['a', '1', 'b', '2', 'c', '3']
iniArray.sort(key=str, reverse=False)
print(iniArray)
Output expected :
print(iniArray)
['a', 'b', 'c', '1', '2', '3']
Answers:
Code
iniArray = ['a', '1', 'b', '2', 'c', '3']
sorted(iniArray, key=lambda x: x.isdigit())
Output:
['a', 'b', 'c', '1', '2', '3']
Write a program that sorts an array by its data value python, the output will be like this
Here is my code so far
iniArray = ['a', '1', 'b', '2', 'c', '3']
iniArray.sort(key=str, reverse=False)
print(iniArray)
Output expected :
print(iniArray)
['a', 'b', 'c', '1', '2', '3']
Code
iniArray = ['a', '1', 'b', '2', 'c', '3']
sorted(iniArray, key=lambda x: x.isdigit())
Output:
['a', 'b', 'c', '1', '2', '3']