in reply to Re: sorting an array if sting having _
in thread sorting an array if sting having _

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

  • Comment on Re^2: sorting an array if sting having _

Replies are listed 'Best First'.
Re^3: sorting an array if sting having _
by hdb (Monsignor) on Dec 12, 2013 at 11:22 UTC

    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.