# Sorting an array of hash

**URL:** <https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670>\
**Category:** Ruby\
**Created:** [February 15, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670 "2007-02-15T13:20:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/1 "2007-02-15T13:20:56Z")

</div>

i do have an array of hashes like that :

{type =\> aType, description =\> aDescription, extension =\> aExtension}

i’d like to sort this array by hash[‘type’], how to do that ?

this array represent the MIME Types.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/2 "2007-02-15T13:37:58Z")

</div>

On 15/02/07, Une Bévue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Une Bévue

arrayOfHashes.sort\_by{|hash| hash[‘type’]}

Farrel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/3 "2007-02-15T13:45:59Z")

</div>

Une Bévue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i’d like to sort this array by hash[‘type’], how to do that ?

i did that that way :

mime\_types.sort!{|mime\_type1,mime\_type2|  
mime\_type1[‘type’] \<=\> mime\_type2[‘type’]  
}

it’s working very well 😉

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/4 "2007-02-15T14:08:01Z")

</div>

> is arrayOfHashes a special object in ruby ???

arrayOfHashes is your array of hashes. You’re calling it arr  
and mime\_types; he’s calling it arrayOfHashes.

> i didn’t found #sort\_by in the pikaxe pages for Array.

sort\_by is defined in the module Enumerable. This module is  
included (mixed-in?..) in Array.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/5 "2007-02-15T13:51:00Z")

</div>

Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arrayOfHashes.sort\_by{|hash| hash[‘type’]}

fine thanks i did it that way :

arr.sort!{|a1,a2| a1[‘type’] \<=\> a2[‘type’]}

is arrayOfHashes a special object in ruby ???

i didn’t found #sort\_by in the pikaxe pages for Array.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/6 "2007-02-15T14:36:07Z")

</div>

> mime\_types.sort!{|mime\_type1,mime\_type2|  
> mime\_type1[‘type’] \<=\> mime\_type2[‘type’]  
> }
> 
> it’s working very well 😉

But it’s inefficient… (See benchmark below.)

You’re doing a lookup in both hashes in order to compare the  
types. A more efficient technique is to cache theses types.

Sort\_by constructs a temporary array, where each element is an  
array containing the type along with the mime type. Sort\_by  
than sorts this array, and extracts the mime type from the  
result.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

* * *

$ cat test.rb  
require “benchmark”

array =  
(1…100\_000).collect do  
{:thing =\> rand}  
end

Benchmark.bmbm do |bm|  
bm.report(“sort”) do  
10.times do  
array.sort do |h1, h2|  
h1[:thing] \<=\> h2[:thing]  
end  
end  
end

bm.report(“sort\_by”) do  
10.times do  
array.sort\_by do |h|  
h[:thing]  
end  
end  
end  
end

* * *

$ ruby test.rb  
Rehearsal -------------------------------------------  
sort 76.360000 0.000000 76.360000 ( 76.510000)  
sort\_by 7.391000 0.000000 7.391000 ( 7.391000)  
--------------------------------- total: 83.751000sec

```
           user system total real

```

sort 46.967000 0.000000 46.967000 ( 47.047000)  
sort\_by 7.191000 0.000000 7.191000 ( 7.190000)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 15, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/sorting-an-array-of-hash/85670/7 "2007-02-15T15:01:10Z")

</div>

Erik V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But it’s inefficient… (See benchmark below.)

it was a “one time running” script then…

> You’re doing a lookup in both hashes in order to compare the  
> types. A more efficient technique is to cache theses types.
> 
> Sort\_by constructs a temporary array, where each element is an  
> array containing the type along with the mime type. Sort\_by  
> than sorts this array, and extracts the mime type from the  
> result.

Ok i see.

> gegroet,  
> Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

Anyway, thanks a lot i’ve learned something today )))
