ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:
When I have an array which comprises both strings and integers , then applying the sort function in below mentioned ways , gives different output. Could anyone please explain me on what basis the sorting is happening in the following two cases :
@arr = ("jack", 80, "martin", 3, "allan", 'george'); @sort = sort { $a cmp $b } @arr; print "@sort \n";
output :
3 80 allan george jack martin
If the sort function is changed to
@sort = sort { $a <=> $b } @arr ;
then the output changes to
jack martin allan george 3 80
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: sort behavior explanation required
by davido (Cardinal) on Apr 20, 2014 at 16:13 UTC | |
by Laurent_R (Canon) on Apr 20, 2014 at 17:38 UTC | |
by AnomalousMonk (Archbishop) on Apr 20, 2014 at 18:35 UTC | |
by Laurent_R (Canon) on Apr 20, 2014 at 19:00 UTC | |
by Laurent_R (Canon) on Apr 21, 2014 at 22:42 UTC | |
by AnomalousMonk (Archbishop) on Apr 22, 2014 at 18:53 UTC | |
| |
by AnomalousMonk (Archbishop) on Apr 20, 2014 at 16:27 UTC | |
Re: sort behavior explanation required
by lidden (Curate) on Apr 20, 2014 at 15:06 UTC | |
Re: sort behavior explanation required
by AnomalousMonk (Archbishop) on Apr 20, 2014 at 16:03 UTC | |
Re: sort behavior explanation required
by Anonymous Monk on Apr 21, 2014 at 06:24 UTC |
Back to
Seekers of Perl Wisdom