Nibaga
Nibaga

Reputation: 25

Alphabetical order without using sort?

while menu == "a":
    name = str(input("Digite o nome do contato: "))
    num = int(input("Digite o número do contato: "))
    list = list + [[name] + [num]]
    menu = input("Digite (a) se deseja adicionar outro contato: ")
print(list)

For example if the list is [['def', 456], ['abc', 123]] How do I put in alphabetical order as soon as I input the name without having to sort() after (I want it to display [['abc', 123], ['def', 456]] ) My teacher said its possible but I don't know how

Upvotes: 0

Views: 56

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49803

Assuming the list is sorted before you add the new value, you search the list to find out where the new value goes, and put it there. (This is the basis behind the Insertion Sort algorithm.)

Upvotes: 1

Related Questions