well if the contents of the array are '1,fresh' and '2,testurl' I think that'll try to do a numerical sort on the pair of strings which wouldn't do anything. I have tried { $b <=> $a } and it didn't work.
I want the sort to take the two strings and sort the strings but only sort by the numerical part. Regards Christopher Brian Tillman wrote: > I'm probably missing something, but what's wrong with?: > > sort {$b <=> $a} @array; > > > > On Dec 15, 2008, at 6:33 PM, Christopher Yee Mon > <christopher.yee...@gmail.com> wrote: > >> I have an array of strings whose members consist of a number followed >> by a comma followed by a text string >> >> e.g. >> 1,fresh >> 2,testurl >> >> I want to sort by descending numerical order according to the number >> part so I made this sort subroutine >> >> sub by_counter_field { >> my($a, $b) = @_; >> $a =~ s/^(.*?),.*/$1/g; >> $b =~ s/^(.*?),.*/$1/g; >> >> if ($a > $b) { -1 } elsif ($a < $b) { 1 } else { 0 } >> } >> >> and sorted it with >> sort by_counter_field @array; >> and it didn't work. >> >> I've also tried replacing >> if ($a > $b) { -1 } elsif ($a < $b) { 1 } else { 0 } >> with >> $b <=> $a >> and it still didn't work. >> >> I also tried this >> sort { ($b =~ /(.*?),.*/)[0] <=> ($a =~ /(.*?),.*/)[0] } @array; >> and it didn't work either. >> >> I can't figure it out for the life of me >> >> Any assistance would be deeply appreciated. >> >> Regards >> >> -- >> To unsubscribe, e-mail: beginners-unsubscr...@perl.org >> For additional commands, e-mail: beginners-h...@perl.org >> http://learn.perl.org/ >> >> >> >> > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/