Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort tuples by given element in Julia?

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?

like image 207
Alec Avatar asked May 06 '20 04:05

Alec


People also ask

How do you sort the list of tuples by an element?

In python, to sort list of tuples by the first element in descending order, we have to use the sort() method with the parameter ” (reverse=True) “ which will sort the elements in descending order.

Are tuples mutable Julia?

Arrays and Tuples This type is completely mutable. We can change the dimensions of it, we can push data to it. For that reason, in most cases arrays are probably the type of data you will want to work with in Julia so long as you need to change dimensions.


1 Answers

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?

like image 102
Oscar Smith Avatar answered Oct 20 '22 18:10

Oscar Smith