So in my class the professor gave us this insertion-based sorting code, but I need it to sort lists that have numbers and text.
I've already tried using if isinstance conditions, but it seems the problem is in the while loop
def insertion_sort(vector):
print(vector)
for i in range(1,len(vector)):
actual=vector[i]
j=i
while j>0 and actual<vector[j-1]: #This is where the problem seems
vector[j]=vector[j-1] #to be.
j=j-1
vector[j]=actual
return vector
This is the output error message:
Traceback (most recent call last):
File "F:\Python\sortactivity.py", line 53, in <module>
print(insertion_sort(text))
File "F:\Python\sort.py", line 9, in insertion_sort
while j>0 and actual<vector[j-1]:
TypeError: '<' not supported between instances of 'int' and 'str'
The testing list that I have is:
lst=['s9','d5',9,5,1,24,12,'test',21]