in reply to Sorting array by number of occurences of a char
Use tr/// to count the commas and use a Schwartzian Transform to limit the amount of work that has to be done.
#!/usr/bin/perl my @unsorted = qw(a,b,c c,b,a a,a,a,a b,b); my @sorted = map { $_->[1] } sort { $b->[0] <=> $a->[0] } map { [ tr/,// , $_ ] } @unsorted; print "@sorted\n"
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting array by number of occurences of a char
by zigdon (Deacon) on Oct 04, 2002 at 15:26 UTC | |
by sauoq (Abbot) on Oct 04, 2002 at 15:43 UTC |