Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: What makes an array sorted and a hash unsorted?

by ig (Vicar)
on Jun 07, 2009 at 02:38 UTC ( [id://769215]=note: print w/replies, xml ) Need Help??


in reply to What makes an array sorted and a hash unsorted?

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.

  • Comment on Re: What makes an array sorted and a hash unsorted?

Replies are listed 'Best First'.
Re^2: What makes an array sorted and a hash unsorted? (fleas)
by tye (Sage) on Jun 07, 2009 at 08:43 UTC
    A hash could be defined as an ordered list of key/value pairs.

    I'll amplify your objections to your own offered premise. Since inserting a new key into a hash can significantly change the "order" of the previously inserted keys, I disagree that a Perl hash can be reasonably characterized as "an ordered list of key/value pairs". Certainly, at any given moment, the key/value pairs in an ordinary Perl hash have a defined order. And that particular order is quite useless, incidental, out of our control, and even inconstant. An "ordered" data structure mustn't shuffle its contents simply because a new item is being inserted. It is impossible to store items in computer memory and not have those items in some "order". That doesn't make every data structure into an "ordered data structure".

    Since it is possible for %one= %two to leave %one with its keys in a different order than %two's, a Perl hash isn't "an ordered list of key/value pairs". Copying a hash doesn't (always) copy the order because the order is only an incidental part of the data structure.

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://769215]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found