http://www.perlmonks.org?node_id=581282


in reply to Sorting a File which contains numbers

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^2: Sorting a File which contains numbers
by johngg (Canon) on Oct 30, 2006 at 14:50 UTC
    Your solution is going to fail as soon as you encounter numbers that are more than single-digits because the default sort that you use is lexical, not numeric. Consider

    perl -le '@arr = (2, 17, 8, 5, 26); print for sort @arr;'

    produces

    17 2 26 5 8

    whereas

    perl -le '@arr = (2, 17, 8, 5, 26); print for sort {$a <=> $b} @arr;'

    does a numerical sort and produces

    2 5 8 17 26

    To sort descending numerically change the anonymous sort routine to {$b <=> $a} rather than using reverse.

    Cheers,

    JohnGG