On Fri, 17 Jun 2016 14:33:12 -0700
Kenneth Wolcott <kennethwolc...@gmail.com> wrote:

> Hi;
> 
>   I'm having trouble understanding the built-in Perl sort with regards
> to mixed numbers and strings
> 
>   I'm looking at http://perldoc.perl.org/functions/sort.html
> 
>   I have an array that I want to have sorted numerically and
> descending.
[...]
>   What I did as a workaround was to implement my own extremely
> brute-force sort routine, which works, but is very ugly.
[...]
>   I'd rather that my code be correct, intuitive and elegant (and
> efficient).

Personally, I'd skip Perl's built-in sort for this and look at using
Sort::Naturally - it handles just this case cleanly and simply, and
will leave your code readable.

e.g.:

my @strings = (
    '999 zzz',
    '111 zzz',
    '111 aaa',
    '999 aaa',
);

print join "\n", Sort::Naturally::nsort @strings;

... would get you the output I think you want, i.e.:

  111 aaa
  111 zzz
  999 aaa
  999 zzz


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to