# Finding the position on an item in a collection

**URL:** <https://www.ruby-forum.com/t/finding-the-position-on-an-item-in-a-collection/113724>\
**Category:** Rails\
**Created:** [September 4, 2007, 11:44am UTC](https://www.ruby-forum.com/t/finding-the-position-on-an-item-in-a-collection/113724 "2007-09-04T11:44:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 4, 2007, 11:44am UTC](https://www.ruby-forum.com/t/finding-the-position-on-an-item-in-a-collection/113724/1 "2007-09-04T11:44:12Z")

</div>

This feels like it should be easy but i can’t work it out: if i have a  
collection of stories, ordered by (for example) date, then how do i find  
out the position of the story with id = (eg) 23 in the collection?

I guess this is a ruby question rather than a rails-specific question,  
but maybe there’s a rails way…

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 4, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/finding-the-position-on-an-item-in-a-collection/113724/2 "2007-09-04T16:48:23Z")

</div>

There must be a better way than this, but it’ll work 😉

You can try iterating trough the collection with a counter, and setting  
the position value on the hit. Something like this:

counter = 0  
position = 0

for story in @stories  
if story.id == 23  
position = counter  
end  
counter = counter + 1  
end

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 4, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/finding-the-position-on-an-item-in-a-collection/113724/3 "2007-09-04T17:06:13Z")

</div>

Ivan Trajkovic wrote:

> There must be a better way than this, but it’ll work 😉
> 
> You can try iterating trough the collection with a counter, and setting  
> the position value on the hit. Something like this:
> 
> counter = 0  
> position = 0
> 
> for story in @stories  
> if story.id == 23  
> position = counter  
> end  
> counter = counter + 1  
> end

Thanks ivan, i ended up doing something similar but a little bit nicer  
after a suggestion elsewhere:

@stories.each\_with\_index do |item, index|  
if item.id == 23  
position = index  
break  
end  
end

thanks for helping though 🙂

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/finding-the-position-on-an-item-in-a-collection/113724/4 "2007-09-25T23:10:41Z")

</div>

Here is my 2cents worth, but I would love to learn a better way.
