# Noob ?: sorting hashes within an array by another array

**URL:** <https://www.ruby-forum.com/t/noob-sorting-hashes-within-an-array-by-another-array/107742>\
**Category:** Rails\
**Created:** [June 25, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/noob-sorting-hashes-within-an-array-by-another-array/107742 "2007-06-25T22:09:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 25, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/noob-sorting-hashes-within-an-array-by-another-array/107742/1 "2007-06-25T22:09:34Z")

</div>

I have two arrays:

```
@new_array_ids = params[@resource_key].map { |k| k.to_i }

@new_array_us = ProjectAssignment.find(@new_array_ids)

```

Let’s say:

@new\_array\_ids ----\> [5,3]

@new\_array ----\>  
[#\<ProjectAssignment:0x4706cd8 @attributes={“id”=\>“3”,  
“resource\_id”=\>“1”}\>, #\<ProjectAssignment:0x4706c4c  
@attributes={“id”=\>“5”,“resource\_id”=\>“4”}\>]

I want the hashes in @new\_array to be sorted by @new\_array\_ids. At first  
I thought I would just be able to use :order =\> @new\_array\_ids, or  
something like that in the find. But the :order option only takes SQL  
fragments (?). So in order to get something like:

@new\_array\_sorted ----\>  
[#\<ProjectAssignment:0x4706c4c  
@attributes={“id”=\>“5”,“resource\_id”=\>“4”}\>,  
#\<ProjectAssignment:0x4706cd8 @attributes={“id”=\>“3”,  
“resource\_id”=\>“1”}\>]

I need to identify the position of each element in @new\_array\_ids, find  
each of the attributes whose id matches a value in @new\_array\_ids, sort  
by the order of values in @new\_array\_ids.

I new to ruby + rails, so please let me know if there’s a really simple  
way of doing this. Thanks.

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 26, 2007, 3:14am UTC](https://www.ruby-forum.com/t/noob-sorting-hashes-within-an-array-by-another-array/107742/2 "2007-06-26T03:14:51Z")

</div>

Thanks a lot! That’s really great! Also, thanks for letting me know  
about Symbol#to\_proc; it’s really concise.

> @new\_array\_us = ProjectAssignment.find(@new\_array\_ids).  
> sort\_by {|pa| @new\_array\_ids.index(pa.id)}

> You can even simplify that bit using Symbol#to\_proc
> 
> @new\_array\_ids = params[@resource\_key].map(&:to\_i)

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [June 25, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/noob-sorting-hashes-within-an-array-by-another-array/107742/3 "2007-06-25T23:42:50Z")

</div>

On Jun 25, 2007, at 4:09 PM, Patrick Berkeley wrote:

> @new\_array ----\>  
> @new\_array\_sorted ----\>
> 
> I new to ruby + rails, so please let me know if there’s a really  
> simple  
> way of doing this. Thanks.
> 
> –

Well, it’s really not too bad.

@new\_array\_us = ProjectAssignment.find(@new\_array\_ids).  
sort\_by {|pa| @new\_array\_ids.index(pa.id)}

(which I’d have put on a single line if it weren’t for the inevitable  
line-wrapping in the email)

In fact, if you didn’t want to sort the results, you could have  
skipped the mapping .to\_i since ActiveRecord will treat .find(“3”)  
and .find(3) the same.

You can even simplify that bit using Symbol#to\_proc

@new\_array\_ids = params[@resource\_key].map(&:to\_i)

Enjoy!

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
