How to sort a list in python alphabetically without using sort?
Answers
Attached is a program that compares the unicode numbers (similar to ASCII) for each letter and inserts them into an array accordingly. So it basically is sorting according to integers rather than letters. It doesn't distinguish between capitals and lowercase letters.
Python automatically treats strings as unicode so you can simply write
'A' < 'B'
and it will output True.
I assume this is a specific assignment, but otherwise python comes with two different sorting mechanisms: .sort() and sorted(), which operate differently so you may want to check those out.
Hope this helps!
"The main program will be,
def quicksort(lst):
if not lst:
return []
return (quicksort([x for x in lst[1:] if x < lst[0]])
+ [lst[0]] +
quicksort([x for x in lst[1:] if x >= lst[0]]))
follow the code with,
unsort_list = ['B', 'D', 'A', 'E', 'C']
sort_list = quicksort(unsort_list)
sort_list
> ['A', 'B', 'C', 'D', 'E']
"