in reply to Re^2: sorting an array if sting having _
in thread sorting an array if sting having _
So really you want to split your strings on the underscore and then sort by the first part, and if they are equal, by the second part? The Schwartzian Transform helps you to do this:
my @sortedFoods = map { $_->[0] } sort { $a->[1] cmp $b->[1] or ($a->[2]//'') cmp ($b->[2]//'') } map { [ $_, split /_/ ] } @Foods;
Note that this sorts the pieces like strings and ignores the fact that some parts of them are numbers. So abc10 comes before abc2.
Update: added parentheses to take into account precedence of //. Thanks choroba!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: sorting an array if sting having _
by choroba (Cardinal) on Dec 12, 2013 at 11:30 UTC | |
|
Re^4: sorting an array if sting having _
by saiprathapreddy (Initiate) on Dec 12, 2013 at 12:05 UTC |