# Ordering after a inject

**URL:** <https://www.ruby-forum.com/t/ordering-after-a-inject/59389>\
**Category:** Rails\
**Created:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/ordering-after-a-inject/59389 "2006-05-03T18:59:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/ordering-after-a-inject/59389/1 "2006-05-03T18:59:41Z")

</div>

Hi,

My results are getting out of order after I do the following command:

count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =  
row[‘count’].to\_i;  
hsh }

Why?

Here are more specifics:

My complete method is this (based off of acts\_as\_taggable code - not’  
DHHs,  
but the original one):

def self.sql\_to\_count\_plays(lookback)  
sql = [“SELECT t.name as name, COUNT(\*) as count  
FROM histories h, tags t  
WHERE h.tag\_id = t.id  
AND created\_on \> ?  
GROUP BY tag\_id  
ORDER BY name”, lookback]  
result = find\_by\_sql(sql)  
count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =  
row[‘count’].to\_i; hsh }  
count  
end

The “result” is in the proper alpha order:  
=\> [#\<Tag:0x3606ab0 @attributes={“name”=\>“baseball”, “count”=\>“2”}\>,  
#\<Tag:0x360  
6a68 @attributes={“name”=\>“name”, “count”=\>“29”}\>, #\<Tag:0x3606900  
@attributes={  
“name”=\>“new”, “count”=\>“3”}\>, #\<Tag:0x36065b8 @attributes={“name”=\>“new  
test fo  
r tags”, “count”=\>“4”}\>, #\<Tag:0x3606210 @attributes={“name”=\>“serial”,  
“count”=

> “4”}\>, #\<Tag:0x3605fd0 @attributes={“name”=\>“test”, “count”=\>“1”}\>]

but once it is processed through the result.inject function it gets out  
of  
alpha order:  
=\> {“new”=\>3, “name”=\>29, “new test for tags”=\>4, “serial”=\>4,  
“test”=\>1, "  
baseball"=\>2}

I can’t figure out why or how to correct it. I don’t have the latest  
pickaxe  
book which documents the inject function and can’t find much  
documentation  
on it on the web. I could turn it into an sorted array using “count =  
count.sort\_by { |r| r.name }”, but I don’t want it in an array. In need  
it  
in a hash.

Any ideas?

Kind regards,

Steve O.  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/ordering-after-a-inject/59389/2 "2006-05-03T18:59:49Z")

</div>

On Tue, May 02, 2006 at 10:12:11AM -0500, Steve O. wrote:  
} My results are getting out of order after I do the following command:  
}  
} count = result.inject({}) { |hsh, row| hsh[row[‘name’]] =  
row[‘count’].to\_i;  
} hsh }  
}  
} Why?  
[…]  
} but once it is processed through the result.inject function it gets  
out of  
} alpha order:  
} =\> {“new”=\>3, “name”=\>29, “new test for tags”=\>4, “serial”=\>4,  
“test”=\>1, "  
} baseball"=\>2}  
}  
} I can’t figure out why or how to correct it. I don’t have the latest  
pickaxe  
} book which documents the inject function and can’t find much  
documentation  
} on it on the web. I could turn it into an sorted array using “count =  
} count.sort\_by { |r| r.name }”, but I don’t want it in an array. In  
need it  
} in a hash.

Hashes are unordered containers. They do not maintain order. That is not  
what they do.

} Any ideas?  
} Kind regards,  
} Steve O.  
–Greg

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/ordering-after-a-inject/59389/3 "2006-05-03T18:59:53Z")

</div>

Aww. Thanks.

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/ordering-after-a-inject/59389/4 "2006-05-03T19:05:34Z")

</div>

Hello Steve,

> - not’ DHHs, but the original one):
> 
> but once it is processed through the result.inject function it gets out of  
> alpha order:  
> =\> {“new”=\>3, “name”=\>29, “new test for tags”=\>4, “serial”=\>4, “test”=\>1, "  
> baseball"=\>2}
> 
> I can’t figure out why or how to correct it. I don’t have the latest pickaxe  
> book which documents the inject function and can’t find much documentation  
> on it on the web. I could turn it into an sorted array using “count =  
> count.sort\_by { |r| r.name }”, but I don’t want it in an array. In need it  
> in a hash.

Gregory has answered you but here some additional information :

1/ if you have my\_hash = MyModel.sql\_to\_count\_plays(lookback)

you can convert into a “sort of ordered hash like object”, in fact an  
array of nested pairs, with Hash#sort method :

irb(main):001:0\> my\_hash={“new”=\>3, “name”=\>29, “new test for  
tags”=\>4, “serial”=\>4, “test”=\>1, “baseball”=\>2}.sort  
=\> [[“baseball”, 2], [“name”, 29], [“new”, 3], [“new test for tags”,  
4], [“serial”, 4], [“test”, 1]]

then you can iterate on each pair :

> my\_hash.each { |p| puts “#{p.first}(#{p.last})” }  
> baseball(2)  
> name(29)  
> new(3)  
> new test for tags(4)  
> serial(4)  
> test(1)

2/ There is an OrderedHash class in Rails.  
In 1.0, it was class OrderedOptions, then it has been refactored a  
little  
bit OrderedOptions \< OrderedHash in 1.1.  
In trunk, it’s called (very recently) ActiveSupport::OrderedHash.

If you look in : activesupport/lib/active\_support/ordered\_options.rb  
you will see that’s basically a “nested pairs in array” structure  
encapsulated in an OrderedHash instance.

HTH,

```
-- Jean-FranÃ§ois.
```

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/ordering-after-a-inject/59389/5 "2006-05-03T19:05:40Z")

</div>

Thanks for this, Jean-Francois. This is helpful. I’ll check it out.

Steve
