in reply to (jeffa) Re: Substring Sort
in thread Substring Sort

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)

Replies are listed 'Best First'.
Re: Re: (jeffa) Re: Substring Sort
by chipmunk (Parson) on Jun 01, 2001 at 05:21 UTC
    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.