How do I sort tuples by given element in Julia?
Asked Answered
C

1

9

If I have a list/array of tuples like this: [(15, 36, 39), (9, 40, 41)]

How do I sort these by the first element? By the last element? By their sum?

Corneliuscornell answered 6/5, 2020 at 4:25 Comment(0)
T
13
x=[(15, 36, 39), (9, 40, 41)]
sort(x, by=x->x[1])
sort(x, by=x->x[end])
sort(x, by=sum)

This answer your questions?

Thyroiditis answered 6/5, 2020 at 4:32 Comment(2)
You could also write first / last instead of x->x[1] / x->x[end], depending on what you think looks nicerPseudohermaphrodite
Also as of 1.5, you can write x[begin]Thyroiditis

© 2022 - 2024 — McMap. All rights reserved.