How to sort an array which has factors as ((x1, y1), (x2, y2)), according to each of lengths in Julia

Viewed 74

I want to sort an array which has factors as ((x1, y1), (x2, y2)) and represents both of edges of a line, according to each of lengths in Julia.
I cannot write because of dim-difference between Python and Julia.
In Python, lines are sorted like follows;

lines = [ ((l[0][0], l[0][1]), (l[0][2], l[0][3])) for l in lines ]
lines.sort(key = lambda l: -sqrt((l[0][0]-l[1][0])**2+(l[0][1]-l[1][1])**2))

How to write the same process in Julia?

1 Answers

It seems you want this:

lines = [((a,b), (c,d)) for (a,b,c,d) in lines]

or this

lines = [((a,b), (c,d)) for (a,b,c,d) in eachrow(lines)]

(I would need an example of input lines and expected output example to be sure)

In order to sort it just use by kwarg:

sort!(lines, by=(((a,b), (c,d)),) -> -sqrt((a-c)^2+(b-d)^2))

or

sort!(lines, by=x -> -sqrt((x[1][1]-x[2][1])^2+(x[2][1]-x[2][2])^2))
Related