It always bothers me when someone says hash are unsorted.

I have more difficulty with the idea that an array is sorted than with the idea that a hash is unsorted.

Thinking only of the data structures and not the interfaces or implementation, I think of an array as an ordered list. Because it is ordered it is possible, though not necessary, for it to be sorted. On the other hand, I think of a hash as an unordered set of key/value pairs. with the constraint that there can only be one of each key in the set. As there is no order, there is no possibility of there being a sorted order.

A hash could be defined as an ordered list of key/value pairs. If it were ordered, then it could be sorted. But very little in the description or interfaces suggests to me that it is ordered and what I know of the implementation is that, to the extent that it is ordered, the order of the elements is randomized (update randomized isn't the right word at all. The distribution is highly ordered in the sense that there should be uniform distribution across the buckets, and the determination is repeatable so that elements can be found again later, so there is nothing unpredictable (i.e. random) about it. Maybe chaotic, in the sense of deterministic chaos, would be a better term - implying that the resulting order is very sensitive to small variations in the initial key values and not easily predicted except by full execution of the hashing algorithm.).

So, arrays are inherently ordered while hashes are either unordered or, at best, ordered but with an inherently random chaotic order. Therefore, arrays are sortable and hashes are inimical to sorting.


In reply to Re: What makes an array sorted and a hash unsorted? by ig
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.