in reply to Re^2: Alphanumeric sort
in thread Alphanumeric sort
If you don't mind reformatting the data slightly you can run a quick regex across the data to reverse the order of the alphanumeric and numeric data.
chomp(my @unsorted = (<DATA>)); map{ $_ =~ s/(\d+),(\w+)/$2,$1/ } @unsorted; my @sorted = sort { $a cmp $b } @unsorted; say for @sorted;
Or do you want to do a complex sort without reformatting? In which case you can do something like the below, which will sort without reformating.
chomp(my @unsorted = (<DATA>)); my @sorted = sort { (split(',',$a))[1] cmp (split(',',$b))[1] || (split(',',$a))[0] cmp (split(',',$b))[0] } @unsorted; say for @sorted;
But again, if your ultimate goal is simple sums of each alphanumeric code, there is no need to sort unless you are outputting each sum as you go and purging it from memory. If holding all the sums in memory at one time is not a problem, summing into a hash should be all you need as I did in my previous example.
|
|---|