linuxfan has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to sort an array that has strings in the format "1-1-1", "1-1-2", "2-3-1" etc. in the ascending order such that 1-1-1 appears before 1-1-2 and so on. Think of each string representing a "chapter-subchapter-verse". I tried the following snippet but it doesn't give me the desirable result.
my @a = ("1-1-2", "6-1-2", "3-1-4"); my @b = sort { (split /-/, $a)[0] <=> (split /-/, $b)[0] && (split /-/, $a)[1] <=> (split /-/, $b)[1] && (split /-/, $a)[2] <=> (split /-/, $b)[2] } @a; print "@b\n";
Obviously something's lacking in my understanding of writing a custom sort function. Can someone help me out?
Many thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Custom sort with string of numbers
by AnomalousMonk (Archbishop) on Aug 29, 2010 at 05:45 UTC | |
|
Re: Custom sort with string of numbers
by Anonymous Monk on Aug 29, 2010 at 05:03 UTC | |
|
Re: Custom sort with string of numbers
by Marshall (Canon) on Aug 29, 2010 at 06:16 UTC | |
by Anonymous Monk on Aug 29, 2010 at 06:46 UTC | |
by Marshall (Canon) on Aug 29, 2010 at 07:28 UTC | |
by JavaFan (Canon) on Aug 29, 2010 at 14:46 UTC | |
by Marshall (Canon) on Aug 30, 2010 at 21:09 UTC | |
by linuxfan (Beadle) on Aug 29, 2010 at 15:17 UTC | |
|
Re: Custom sort with string of numbers
by salva (Canon) on Aug 29, 2010 at 08:51 UTC |