my @sorted_points = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map {[$_, dist($_, $reference_point)]} @points; # points is an AoA structure