in reply to Sorting complex records

If, as appears likely, the first field is an ID number (ie always the two letters 'ID' followed by a number), it would be nice to sort these numerically rather than ascibeticallly (ie with ID100 coming after rather than before ID20...).

Adapting the first answer given above, you could do this:

@data = sort { substr ($a->[0], 2) <=> substr ($b->[0], 2) || $a->[3] <=> $b->[3] } @data;

Meanwhile, I've downloaded the Data::Sorting module and have started playing with it, but haven't yet worked out how this would be done (although I'm sure it's simple :-).

dave

Replies are listed 'Best First'.
Re: Re: Sorting complex records
by simonm (Vicar) on Sep 17, 2003 at 22:09 UTC
    Data::Sorting allows you to pass in a subroutine reference that is responsible for extracting the value to sort on:
    sort_arrayref( $data, -compare=>'numeric', sub { substr (shift)->[0] +, 2 }, 3 )

    There's also a (poorly-documented) interface to let you specify this in data rather than code:

    sort_arrayref( $data, -compare=>'numeric', -extract=>'compound', [ index => 0, substr => 2 ], [ index => 3 ] )