I want to sort this DATA from highest to lowest, on the first field. The sort works but only on the first number. For instance 9 is greater than 66 and 6 is greater than 57. If two fields are the same number, it will only print one of them, and I don't know where the extra characters come from at the end of the output. Please help.
print "Content-type: text/html\n\n"; while(<DATA>) { chomp; @sorted=(); @sorted = split(/\|/); $sorted{$sorted[0]} = [@sorted]; } print "$_->[0]|$_->[1]|$_->[2]<BR>\n" for sort {$b->[0] cmp $a->[0]} v +alues %sorted; __DATA__ 24|index.htm|http://localhost/~owner/index.htm 18|alertBoxes.htm|http://localhost/~owner/alertboxes.htm 4|andrewsGame.htm|http://localhost/~owner/andrewsgame.htm 3|bounceGame.htm|http://localhost/~owner/bouncegame.htm 21|browserRace.htm|http://localhost/~owner/browserrace.htm 6|breakFrames.htm|http://localhost/~owner/breakframes.htm 44|colorTables.htm|http://localhost/~owner/colortables.htm 61|humanReadable.htm|http://localhost/~owner/humanreadable.htm 27|mouseXY.htm|http://localhost/~owner/mousexy.htm 9|calculator.htm|http://localhost/~owner/calculator.htm 15|fakeFormat.htm|http://localhost/~owner/fakeformat.htm 2|makeCircle.htm|http://localhost/~owner/makecircle.htm 22|imageSwap.htm|http://localhost/~owner/imageswap.htm 5|easyDate.htm|http://localhost/~owner/easydate.htm 66|encoder.htm|http://localhost/~owner/encoder.htm 57|bgChanger.htm|http://localhost/~owner/bgchanger.htm 10|centipede.htm|http://localhost/~owner/centipede.htm 11|cookies.htm|http://localhost/~owner/cookies.htm

The output of this script is:

9|calculator.htm|http://localhost/~owner/calculator.htm 66|encoder.htm|http://localhost/~owner/encoder.htm 61|humanReadable.htm|http://localhost/~owner/humanreadable.htm 6|breakFrames.htm|http://localhost/~owner/breakframes.htm 57|bgChanger.htm|http://localhost/~owner/bgchanger.htm 5|easyDate.htm|http://localhost/~owner/easydate.htm 44|colorTables.htm|http://localhost/~owner/colortables.htm 4|andrewsGame.htm|http://localhost/~owner/andrewsgame.htm 3|bounceGame.htm|http://localhost/~owner/bouncegame.htm 27|mouseXY.htm|http://localhost/~owner/mousexy.htm 24|index.htm|http://localhost/~owner/index.htm 22|imageSwap.htm|http://localhost/~owner/imageswap.htm 21|browserRace.htm|http://localhost/~owner/browserrace.htm 2|makeCircle.htm|http://localhost/~owner/makecircle.htm 18|alertBoxes.htm|http://localhost/~owner/alertboxes.htm 15|fakeFormat.htm|http://localhost/~owner/fakeformat.htm 11|cookies.htm|http://localhost/~owner/cookies.htm 10|centipede.htm|http://localhost/~owner/centipede.htm ||

In reply to Problem with sorting numbers. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.