Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Bubble sorting multi-d arrays

$
0
0
Please help me with this!!! I've looked every where and tried everything but I cannot figure out how to sort by zipcode, the last element of my array. If someone can just explain how to choose which element to sort by, that would be amazing! Thanks for the help.


def main():
    
    a=[['Zdofalos','Fred','Charlotte','NC',28210],
       ['Johnson','Malcolm','Monroe','NC',28337],
       ['Terrell','Monkey','Broken Pine','SC',28974],
       ['Wilson','Wilson','Hogwart','VA',27457],
       ['Key','LeDoor','Spot In Road','AL',36827],
       ['Smith','Jim Bob','Denver','NC',28037],
       ['Alfonso','Ralph','Gastonia','NC',28554]]
    sort=0
    sort=input("What would you like to sort? 1)LastName  2)Zipcode 3)Exit")
    if sort == 1:
        bubble_sort(a)
        print a
        main()

    elif sort == 2:
        zip_sort(a)
        print a
        main()
    elif sort == 3:
        end()
    
def bubble_sort ( a ) :
   for i in xrange(len(a)):
    for j in xrange(i+1,len(a)):
        if a[i]>a[j]:
           tmp = a[i]
           a[i] = a[j]
           a[j] = tmp 
    
#This is where I need help!!!  I'm not even sure what is being sorted by this code.    
def zip_sort(a):
    for i in xrange(len(a[4])):
        for j in xrange(i+1,len(a[4])):
            if a[i]>a[j]:
                tmp=a[i]
                a[i]=a[j]
                a[j]=tmp
        
    
def end():
    #funtion to end
    stop='y'
    stop=raw_input ("Are you sure that you want to end this program?  y/n: ")
    
    while stop == 'y':
        print 'goodbye'
        break

    else:
        main()
        # if the user wants to keep going, the program will be sent back to the begining of the main module.    
main()


Mod edit:
Please remember to use [code] tags when posting code examples:

:code:

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>