I have an array of arrayrefs with this structure:
@array = ( [ '20100403', 'text', 'account code' ],
[ '19990503', 'text', 'other account code' ],
... etc )
I want to sort by account code and then by date.
I read in various
places that the following will do the trick:
@array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
But I'm not getting the sort order I want. I know that the "sort"
function requires a three-valued return from the block, -1, 0 or +1;
but
the "||" operator won't do that; it returns TRUE or FALSE.
Is that understanding correct? Is that the reason that I'm not getting
the order I want?
I have an array of arrayrefs with this structure:
@array = ( [ '20100403', 'text', 'account code' ],
[ '19990503', 'text', 'other account code' ],
... etc )
I want to sort by account code and then by date. I read in various
places that the following will do the trick:
@array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
But I'm not getting the sort order I want. I know that the "sort"
function requires a three-valued return from the block, -1, 0 or +1; but
the "||" operator won't do that; it returns TRUE or FALSE.
Is that understanding correct? Is that the reason that I'm not getting
the order I want?
The code below works as expected:
my @b = sort { $$a[0] cmp $$b[0] || $$a[1] <=> $$b[1] } @a;
I have an array of arrayrefs with this structure:
@array = ( [ '20100403', 'text', 'account code' ],
[ '19990503', 'text', 'other account code' ],
... etc )
I want to sort by account code and then by date. I read in various
places that the following will do the trick:
@array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
But I'm not getting the sort order I want. I know that the "sort"
function requires a three-valued return from the block, -1, 0 or +1; but
the "||" operator won't do that; it returns TRUE or FALSE.
Is that understanding correct? Is that the reason that I'm not getting
the order I want?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 86:12:51 |
Calls: | 6,658 |
Files: | 12,203 |
Messages: | 5,333,786 |