# How does sort work on hash

I am just a little curious about how exactly sort workes on hashes.

for example:
a = {:foo => 10,:bar => 5,:duh => 20}

a.sort {|x,y| y <=> x}

now how exactly the above line works.the block is passed two elements at
a
time, as array? If yes, how sort treates the next two elements?

what algorithm this sort is based on? Is it inplace sorting? or only
sort!
does inplace sorting?

May be the question is not so much about hashes.

On 8/19/06, hemant kumar [email protected] wrote:

I am just a little curious about how exactly sort workes on hashes.

for example:
a = {:foo => 10,:bar => 5,:duh => 20}

a.sort {|x,y| y <=> x}

x and y represent the key, value pairs of the hash and therefore you
sort by
values, descending order…

now how exactly the above line works.the block is passed two elements at
a

time, as array? If yes, how sort treates the next two elements?

… there are no next two elements the block just tells ruby how to
compare any two key, value pairs. Ruby than applies a sorting algorithm
internally…

what algorithm this sort is based on? Is it inplace sorting? or only
sort!

does inplace sorting?

An array is created from the hash which is sorted by quicksort with the
criteria indicated above

May be the question is not so much about hashes.

Exactly, the question is about Enumerations.

Hope that helps
Robert

Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

• Albert Einstein