# Hash Bubble sort?

**URL:** <https://www.ruby-forum.com/t/hash-bubble-sort/140194>\
**Category:** Ruby\
**Created:** [June 11, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/hash-bubble-sort/140194 "2008-06-11T19:39:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 11, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/hash-bubble-sort/140194/1 "2008-06-11T19:39:15Z")

</div>

How does one implement a bubble sort algorithm for a hash?

Thanks

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 11, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/hash-bubble-sort/140194/2 "2008-06-11T19:45:41Z")

</div>

Justin To wrote:

> How does one implement a bubble sort algorithm for a hash?

One does not as hashes are unordered and thus not sortable.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 11, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/hash-bubble-sort/140194/3 "2008-06-11T19:56:16Z")

</div>

Ah, I see! Thanks

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 13, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/hash-bubble-sort/140194/4 "2008-06-13T13:33:42Z")

</div>

Sebastian H. wrote:

> One does not as hashes are unordered and thus not sortable.

Well if the keys can be ordered (e.g. are Comparable), the hash can be  
sorted by keys. If the values can be ordered, the hash can be sorted by  
values. Which you do depends on your requirements.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 13, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/hash-bubble-sort/140194/5 "2008-06-13T17:47:09Z")

</div>

Justin To wrote:

> How does one implement a bubble sort algorithm for a hash?

If you want to write your own sort, you should know that bubble sorts  
are deprecated. To get a good view of types of sorting algorithms, try  
looking at [http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html](http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html)  
as they have show and tell with source code. Granted it is java, but  
you can translate.

bubble\_sort == bad
