Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found