There are two built in sorting operators in Perl - <=> and cmp - which are discussed in Equality Operators. <=> is used for numerical comparison and cmp is used for string comparison. Since the data from your first column are string data, you should use cmp.

If neither operator fits your requirements, you can design your own comparison function, following the instructions in sort.

On a side note, why are you using Text::CSV_XS if you are not using any of the code? Why are you both requiring and using the module when these are redundant operations? Why are you manually stripping leading zeros and why are you still feeding the numeric comparison operator values with leading quotes, even after I pointed out (Re^2: How do I sort a CSV file on multiple columns?) that it doesn't work the way you expect? Why are you posting code that has use strict commented out? See How do I post a question effectively?.

Update: I also just noticed that your @rows array contains strings, not array refs like your sort function expects. The line $result = join (",", @fields); concatenates all of the values in the array @fields into a single, comma-separated string. You then attempt to access each of those strings as if they were array refs, an issue that would have been caught by use strict;. To understand how to use references in Perl, read perlref, perlreftut and/or perllol. Compare your code to the code I've already referenced above to see a prototype of how you should be accessing these data structures.


In reply to Re: Sorting help by kennethk
in thread Sorting help by mmittiga17

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.