I am trying to sort an array containing positive numbers, negative numbers, and zeros using Perl's built-in sort() function. The array returned from this function drops the zeros and changes the negative numbers to positive numbers. Here is what I am trying now:
#!c:\perl\bin use strict; use warnings; my @data = (1,-5,0,-8,2,0,4,7,3,6,9); @data = sortData(@data, "Integer"); sub sortData { my @data = $_[0]..$_[scalar(@_)-2]; my $dataType = $_[scalar(@_)-1]; if($dataType eq 'Integer' || $dataType eq 'Float') { sort {$a <=> $b} @data; } else { sort @data; } }
I was expecting to get (-8, -5, 0, 0, 1, 2, 3, 4, 6, 7, 9), but instead I got (1, 2, 3, 4, 5, 6, 7, 8, 9). Any thoughts?

In reply to A problem sorting numbers with sort() 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.