@ends = sort{ $b->[0]{X} <=> $a->[0]{X} || $b->[0]{Y} <=> $a->[0]{Y} || $b->[2] cmp $a->[2] } @ends;