Sort 2D vector in descending order

Asked

Viewed 216 times

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.

Wish to sort vector elements according to variable values number

For example:

I have the following vector and its values.

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

After the ordination operation, I need him to stay that way:

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

How can I do that?

1 answer

1


Just use the method sort defining the parameter key:

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

See working on Repl.it | Ideone

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

  • Exactly what I needed @Anderson Carlos Woss! Again, thank you so much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.