I'm not the Perl guru that some folk here are, but my understanding of the $a and $b tokens is that they represent two items in the list which must be compared against each other in order to know which should come first in the list. If you had only one item to compare, the list would not be sorted at all, because each item would stay in the same place. The only way to sort is by shifting positions of two items at a time, until the entire list is properly sequenced. The specifications given for the $a and $b simply define what features one wishes to consider when comparing. Do you, for example, wish to rank them by their numerical value? by their string length? by their alphabetical order? ... and so on.

If $a only gets compared to $a, it will not move--and the list will remain unchanged. Sorting only means anything if each item gets compared to another one. On the other hand, it would seem rather strange to compare, for ranking purposes, the numerical value of one item with the string length of another--and the results of such a comparison, if Perl allowed it (I don't know if it would or not), would probably look rather illogical.

Blessings,

~Polyglot~


In reply to Re: Notation of sort function by Polyglot
in thread Notation of sort function by Alphaphi

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.