in reply to Substring Sort

Well, this doens't pass the 'extra array' requirement, but if your data set is small enough, use a Schwartzian Transform:
my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { my ($n,$t) = split('|:|',$_,2); [$_,$t] } @origarray; # change this to sort by lower case: [$_,lc($t)]

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

Replies are listed 'Best First'.
Re: (jeffa) Re: Substring Sort
by arhuman (Vicar) on Jun 01, 2001 at 01:52 UTC
    I can't test it nor benchmark it* but isn't the following code faster ?

    my @sorted = map { my ($n,$t) = split('|:|',$_,2); $t.'|:|'.$n } sort map { my ($n,$t) = split('|:|',$_,2); $t.'|:|'.$n } @origarray;

    The idea behind is that default sort is always faster according to this excellent article about Efficient Perl sorting
    (I couldn't cite this jewel too much...)

    * I'm at home and my Perl environment is curently down

    "Only Bad Coders Code Badly In Perl" (OBC2BIP)
      Careful there... When you use the default sort and have extra data appended to each string, you have to make sure the extra data doesn't affect the sort order.

      In this case, '|' (ASCII value 124) sorts after most other characters, so that code would sort a string before its prefixes. For example, 'string|extra data' comes after 'string plus|extra data', even though 'string' comes before 'string plus'.

      I think the easiest way to solve this problem is by sticking a null character between the sort string and the extra data. (If the data contains null characters already, it takes a bit more work.)

      @origarray = qw/ third|:|bb second|:|b fourth|:|c first|:|a /; my @sorted = map { /\0(.*)/s } sort map { (split /\|:\|/)[-1] . "\0$_" } @origarray; print "@sorted\n";
      That outer map could be done with substr() and index() instead of a regex.
Re: (jeffa) Re: Substring Sort
by MeowChow (Vicar) on Jun 01, 2001 at 02:08 UTC
    Eek, don't forget to escape the pipes in your split. Remember, just because there's quotes around it doesn't mean that it's not getting compiled as a regex.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print