What is 'sorted'? (unsorted) data is sorted after sorting.

An array is sorted because it can return its values in order of ascending keys (indexes).

No. An array is sorted when their values are sorted, by whatever order you impose on them. I consider the following

@ary = (34, 'a', '+', \0, 'y','Z');

as an unsorted array. Iterating over an array via ascending indexes or, in a destructive way, via shift or pop is the natural way to access all values sequentially, just like each does for hashes. Arrays are a defined sequence of scalars on which you can impose any order in a stable way, e.g. by sorting the values

@ary = sort { $b cmp $a } @ary;

meaning that after that operation the sequential iteration over the array whith ascending or descending keys reflects the order which you impose on the data.

That's not possible with hashes. You can't impose a different order of key/value tupels on a hash to change the order in which each returns those tupels. That's meant by 'hashes are not sortable'. If you fill a hash sequentially which tupels, retrieveing those via each doesn't necessarily reflect the initial filling sequence. That's meant by 'hashes are unordered' (or unsorted).

@h{(a..z)} = (A..Z); %h = map { $_ => $h{$_} } sort { $b cmp $a } keys %h;

This may change the order in which each returns tupels iterating over the hash, but not in a predictable way, and certainly their sequence doesn't reflect the sorting.

But you can do that with arrays. That's what makes arrays sorted (if you sort them) but hashes unsorted: you can't impose a different stable order in which tupels are returned.

Sorting applies to a sequence. If we look at hashes as a collection of tupels, we note that we can't influence the ordering of tupels retrieving those as a sequence. But that's due to the fact that arrays are sequences, and hashes are not: "associative array" for perl hashes is a misnomer ;-)


In reply to Re: What makes an array sorted and a hash unsorted? by shmem
in thread What makes an array sorted and a hash unsorted? by ikegami

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.