in reply to Re: Sorting a referrenced array and need to sort by specific fields.
in thread Sorting a referrenced array and need to sort by specific fields.
But in the given sort procedure, you are performing two splits on each comparison. That's quite costly. It's much better to use either a Schwartzian transform, or a Guttman-Rosler-Transform. The former is the simpler, the latter the faster of the two.
Here's the ST:
And here's the GRT:@sorted = map {$_ -> [0]} sort {$a -> [1] cmp $b -> [1] || $a -> [2] cmp $b -> [2] || $a -> [3] cmp $b -> [3]} map {[$_ => split /,/]} @$outputRef;
The reason why the GRT is faster than ST is because the GRT doesn't have a custom sort routine, where ST does. GRT does more pre-processing work than ST, but that's only linear in the amount of elements to sort, while there will be O (N * log N) comparisons made.my ($max_symbol, $max_side, $max_account) = (0, 0, 0); foreach (@$outputRef) { my ($symbol, $size, $account) = split /,/; $max_symbol = length $symbol if length $symbol > $max_symbo +l; $max_size = length $size if length $size > $max_size + ; $max_account = length $account if length $account > $max_accou +nt; } my $total = $max_symbol + $max_size + $max_account; @sorted = map {substr $_, $total} sort map {my ($symbol, $size, $account) = split /,/; $symbol .= "\0" x $max_symbol; $size .= "\0" x $max_size; $account .= "\0" x $max_account; substr ($symbol, 0, $max_symbol) . substr ($size, 0, $max_size) . substr ($account, 0, $max_account) . $_} @$outputRe +f;
Abigail
|
|---|