0

I have the following tuple:

[[278, 388, 0.95434654], [279, 388, 0.98770386], [280, 388, 0.960356], [442, 388, 0.98245245], [443, 388, 0.96607447], [278, 389, 0.95714802], [279, 389, 0.98451048], [280, 389, 0.95131117], [442, 389, 0.98007435], [443, 389, 0.95733315]]

I would like to sort it by first then second then third.

I tried the following but it only sorts by first:

def getKey(item):
    return item[0]

all_matches_tuple = sorted(all_matches_tuple, key=getKey)
Richard
  • 703
  • 3
  • 11
  • 33

1 Answers1

2

Just sort without a key, the default is to sort tuples in lexicographical order:

sorted(all_matches_tuple)

Demo:

>>> sorted([[278, 388, 0.95434654], [279, 388, 0.98770386], [280, 388, 0.960356], [442, 388, 0.98245245], [443, 388, 0.96607447], [278, 389, 0.95714802], [279, 389, 0.98451048], [280, 389, 0.95131117], [442, 389, 0.98007435], [443, 389, 0.95733315]])
[[278, 388, 0.95434654], [278, 389, 0.95714802], [279, 388, 0.98770386], [279, 389, 0.98451048], [280, 388, 0.960356], [280, 389, 0.95131117], [442, 388, 0.98245245], [442, 389, 0.98007435], [443, 388, 0.96607447], [443, 389, 0.95733315]]

but note that your sample doesn't actually contain any values where both the first and second number are equal.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343