Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Sorting a File which contains numbers

by valavanp (Curate)
on Oct 30, 2006 at 13:24 UTC ( #581282=note: print w/replies, xml ) Need Help??

in reply to Sorting a File which contains numbers

You can try this:
use strict; use warnings; my @a=(1,5,2,4); print "Ascending order:"; print sort(@a); print "\n"; print "Descending order:"; print reverse sort(@a);

Replies are listed 'Best First'.
Re^2: Sorting a File which contains numbers
by johngg (Abbot) 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;'


    17 2 26 5 8


    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.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://581282]
[james28909]: ok i have a new question ;)
[james28909]: i am not sure how to ask google this either
[james28909]: i am getting the current time on my machine with scalar localtime;
[james28909]: i am scraping a website and retrieving a date stamp of an article.
[james28909]: well time/data stamp of the article

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-04-29 02:30 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.