but if you are sorting lexicograficaly, you can not compare numbers as numbers because they are not equivalent operations (i.e.: 2 < 10 but 2 gt 10).
It shouldn't be too difficult to write a sorting sub in XS that uses a comparison callback equivalent to the Perl {($c, $d) = ($a, $b); $c cmp $d }. But converting the numbers to strings is an expensive operation, even in C.
Also, you can apply a transformation to the numbers to make them sort lexicograficaly when compared as numbers. For instance, for positive integers with 8 or less digits (untested):
use Sort::Key 'ukeysort_inplace'; my $digits = 8; ukeysort_inplace { my $a = int $_; length $a > $digits and die 'integer $a is too big'; $a .= ' ' x ($digits - length $a); $a =~ tr/ 0123456789/0123456789a/; hex $a } @array;
(usortkey_inplace should not use more than 8 additional bytes per value on a 32bits machine, update: + 4 bytes for the merge phase of the mergesort)
In reply to Re^7: Howto Avoid Memory Problem in List::MoreUtils
by salva
in thread Howto Avoid Memory Problem in List::MoreUtils
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |