$top = [$x_t,$y_t,$z_t]; # you got this from your maxima search @subset = ( [$x0,$y0,$z0], [$x1,$y1,$z1], ...); # the subset in step1 @contour = sort { atan2($a->[1] - $top->[1], $a->[0] - $top->[0]) <=> atan2($b->[1] - $top->[1], $b->[0] - $top->[0]) } @subset;