in reply to sort on numbers embedded in text

Well, the most common problem when sorting text that contains numbers is that values with different numbers of digits don't sort numerically. Here is one way to fix that. Here is another:

my @data= qw( 953-FMT-FMT 954-FMT-FMT BLOGO93-FMT-FMT 955-FMT-FMT BOXSTART2-FMT-FMT BOXEND2-FMT-FMT 956-FMT-FMT 413-FMT-FMT DATE1-FMT-FMT 414-FMT-FMT 415-FMT-FMT 416-FMT-FMT BLOGO107-FMT-FMT 417-FMT-FMT 419-FMT-FMT 418-FMT-FMT BLOGO63-FMT-FMT 393-FMT-FMT 394-FMT-FMT BLOGO75-FMT-FMT 420-FMT-FMT 421-FMT-FMT 395-FMT-FMT ); my %data; foreach my $data ( @data ) { ( my $sort= $data ) =~ s/(0*)(\d+)/ pack("C",length($2)) . $1 . $2 /ge; $data{$sort}= $data; } print join( "\n", @data{ sort keys %data } ), "\n";
which produces
393-FMT-FMT 394-FMT-FMT 395-FMT-FMT 413-FMT-FMT 414-FMT-FMT 415-FMT-FMT 416-FMT-FMT 417-FMT-FMT 418-FMT-FMT 419-FMT-FMT 420-FMT-FMT 421-FMT-FMT 953-FMT-FMT 954-FMT-FMT 955-FMT-FMT 956-FMT-FMT BLOGO63-FMT-FMT BLOGO75-FMT-FMT BLOGO93-FMT-FMT BLOGO107-FMT-FMT BOXEND2-FMT-FMT BOXSTART2-FMT-FMT DATE1-FMT-FMT
note how "BLOGO107" comes after "BLOGO93".

If you also want "BLOGO" to sort last, then either separate those beforehand as already mentioned or try this:

my @data= qw( 953-FMT-FMT 954-FMT-FMT BLOGO93-FMT-FMT 955-FMT-FMT BOXSTART2-FMT-FMT BOXEND2-FMT-FMT 956-FMT-FMT 413-FMT-FMT DATE1-FMT-FMT 414-FMT-FMT 415-FMT-FMT 416-FMT-FMT BLOGO107-FMT-FMT 417-FMT-FMT 419-FMT-FMT 418-FMT-FMT BLOGO63-FMT-FMT 393-FMT-FMT 394-FMT-FMT BLOGO75-FMT-FMT 420-FMT-FMT 421-FMT-FMT 395-FMT-FMT ); my %data; foreach my $data ( @data ) { ( my $sort= $data ) =~ s/(0*)(\d+)/ pack("C",length($2)) . $1 . $2 /ge; $sort =~ s/^BLOGO/~BLOGO/; $data{$sort}= $data; } print join( "\n", @data{ sort keys %data } ), "\n";

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
RE: (tye)Re: sort on numbers embedded in text
by d4vis (Chaplain) on Oct 09, 2000 at 20:47 UTC
    This did the trick.
    Sorted as one column, with ddd-FMT-FMT items followed by the BLOGOddd-FMT-FMT items, then an alphabet sort on the remaining items.
    Many thanks.

    ~d4vis the scribe
    #!/usr/bin/fnord