[section start marker (shift-opt-5)] [some line of text] entry \(frequency data\) entry \(frequency data\) … [section start marker (shift-opt-5)] [and the pattern repeats] #### sub two_way_sort { ($b =~ /\((\d+)/)[0] <=> ($a =~ /\((\d+)/)[0] || lc($a) cmp lc($b) } my @unsorted_input; while (my $line = ) { [section here of some text edits] push @unsorted_input, $line; } my @sorted = sort two_way_sort @unsorted_input;