Sort list lists with two sorting criteria

4

If I have a list of lists like this:

lista = [['ana','1'], ['joao', '3'], ['rita','2'], ['alice','2']]

I first want to sort the list according to the numbers, to look like this:

lista = [['ana','1'], ['rita','2'], ['alice','2'], ['joao', '3']]

What I did with: listaOrdenada = sorted(lista, key = lambda x: x[1])

But as in this case I have two lists with the number '2', I want to sort these two lists alphabetically by name, how do I do this?

    
asked by anonymous 30.11.2015 / 20:41

1 answer

3

You can do so

listaOrdenada = sorted(lista, key = lambda x: (x[1], x[0]))

The last lambda says that the first sort criterion is the 1 column and the second sorting criterion is the 0 column.

    
30.11.2015 / 20:48