How to alphabetically sort 2D array based on an element, without using the built-in sort function? (python) -
i've been stuck on while, 1 please me?
suppose have array:
[[231,cow,234334,2231319,323242],[3,alien,2,2312,3212],[9,box,234,2319,3242]]
can me create sort function sorts array alphabetically based on 2nd element of each individual array in larger array, looks like:
[[3,alien,2,2312,3212],[9,box,234,2319,3242],[231,cow,234334,2231319,323242]]
here simple bubble sort 1 of easiest sorts understand (since looks homework.) key, think, instructor trying think 2d aspect, here means need @ second element of second column in compare. have modified compare this. tested , need put quotes around elements strings.
def bubblesort( ): in range( len( ) ): k in range( len( ) - 1, i, -1 ): if ( a[k][1] < a[k - 1][1] ): swap( a, k, k - 1 ) def swap( a, x, y ): tmp = a[x] a[x] = a[y] a[y] = tmp a=[[231,'cow',234334,2231319,323242],[3,'alien',2,2312,3212],[9,'box',234,2319,3242]] print bubblesort(a) print
Comments
Post a Comment