in reply to sorting an array if sting having _

Please, explain why abc01_1 goes after abc_1, but before abc1. See sort for details on how to sort.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: sorting an array if sting having _
by saiprathapreddy (Initiate) on Dec 12, 2013 at 11:07 UTC

    _ (underscore has taken high priority comapred to other values that is why)

      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!

        Thanks a Lot ..choroba

    A reply falls below the community's threshold of quality. You may see it by logging in.