Sort 2D vector in descending order

1

I have the following vector:

a = [['UK', 'FR', **numero**], ['UK', 'NL', **numero**], ['UK', 'PT', **numero**]]

Where the variable number are floating point values and different.

I want to sort the vector elements according to the values of the number

For example:

I have the following vector and their values.

a = [['UK', 'FR', 0.021], ['UK', 'NL', 0.094], ['UK', 'PT', 0.034]]

After the sort operation, I need it to look like this:

a = [['UK', 'NL', 0.094], ['UK', 'PT', 0.034], ['UK', 'FR', 0.021]]

How can I do this?

    
asked by anonymous 18.05.2018 / 20:54

1 answer

1

Just use the sort method by setting the key parameter:

a.sort(key=lambda l: l[2], reverse=True)

See working at Repl.it | Ideone

Thus, the a list will be sorted taking into account the third value of each list (index 2), in descending order.

    
18.05.2018 / 21:03