use Sort::Key::Multi qw(nn_keysort) # the "nn" prefix means # to use two numeric keys my @data; while () { my @f = /([\d.]+)/g or next; push(@data, \@f); } my @sorted = nn_keysort { $_->[1], $_->[0] } @data; printf("((%f %f) (%f %f))\n", @$_) foreach @sorted;