Python sort nested lists

I have these lists which I have sorted in ascending order via selection sorting but it only applies to index [0]. How would I append it so it sorts the next index as well? The result that I am after is :

[[2, 3], [4, 5], [6, 7]]

So far i have:

def selection_sort(list_a):

    indexing_length = range (0, len(list_a)-1)

    for i in indexing_length:
        min_value = i

        for j in range (i +1, len(list_a)):
            if list_a[j] <list_a[min_value]:
                min_value = j


        if min_value != i:
            list_a[min_value], list_a[i]= list_a[i], list_a[min_value]

    return list_a

list_a = [[5, 4], [2, 3], [6, 7]]

print (selection_sort(list_a))

Upvotes: 0

Views: 79

Answers (1)

kampmani
kampmani

Reputation: 709

If I understood right, you'd like to sort the nested lists individually and then sort the elements of the main list:

print(selection_sort([selection_sort(l) for l in list_a]))

I hope this helps!

Upvotes: 2

Related Questions