Sorting 2d arrays


#1

contacts =[[‘Zebra’,‘Monkey’, ‘Dog’, ‘Bird’, ‘Dodo Bird’],[‘12’, ‘11’, ‘200’, ‘81’, ‘0’]]

def insertionSort(contacts):
for index in range(1,len(contacts[0])):

    name = contacts[0][index]
    num = contacts[1][index]
    position = index

    while position>0 and contacts[0][position-1]>name and :
        contacts[0][position]=contacts[0][position-1]
        contacts[1][position] = contacts[1][position-1]
         
        position = position-1
 
    contacts[0][position]=name

print (contacts)

insertionSort(contacts)

So this is what I have got so far but it is not sorting the way I want it to sort. I want the names to sort and the numbers to move with it. If someone can help me, it would be great.


#2

You could create pairs

['zebra', '12']

And then sort those

Have a look at the zip function, or pair them up with your own code


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.