# Sorting different objects

**URL:** <https://discuss.rubyonrails.org/t/sorting-different-objects/18162>\
**Category:** rubyonrails-talk\
**Created:** [September 14, 2007, 4:39pm UTC](https://discuss.rubyonrails.org/t/sorting-different-objects/18162 "2007-09-14T16:39:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_Adams](https://avatars.discourse-cdn.com/v4/letter/i/0ea827/32.png) [@Iain\_Adams](https://discuss.rubyonrails.org/u/Iain_Adams)\
**Post date:** [September 14, 2007, 4:39pm UTC](https://discuss.rubyonrails.org/t/sorting-different-objects/18162/1 "2007-09-14T16:39:26Z")

</div>

hi

I have an array of topics and posts each has an updated\_on field. I want to sort the array so in the order of the updated\_on field.

I.e

1 Topic updated on 5 march 2 Poll updated on 4 march 3 Topic updated on 6 march 4 poll updated on 7 march

shoud go 4,3,1,2

my code looks like this @topicsAndPolls.sort { |a,b| a.updated\_on \<=\> b.updated\_on} but this isnt working. Do I have to define the \<=\> method somewhere. Can I pass that in the block aswell?

Thanks

Iain Adams

---

<div class="post-metadata">

**Author:** ![Hugh\_Sasse](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Hugh\_Sasse](https://discuss.rubyonrails.org/u/Hugh_Sasse)\
**Post date:** [September 14, 2007, 4:59pm UTC](https://discuss.rubyonrails.org/t/sorting-different-objects/18162/2 "2007-09-14T16:59:33Z")

</div>

> I have an array of topics and posts each has an updated\_on field. I want to sort the array so in the order of the updated\_on field.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]

> my code looks like this @topicsAndPolls.sort { |a,b| a.updated\_on \<=\> b.updated\_on} [\n inserted] but this isnt working. Do I have to define the \<=\> method somewhere. Can I pass

What do you assign the result of this to? Or did you mean &nbsp;&nbsp;@topicsAndPolls.sort! { |a,b| a.updated\_on \<=\> b.updated\_on}

? That's s/sort/sort!/ in sed parlance, in case the addition of one char is as hard for you to spot as for me.

> that in the block aswell?

If updated\_on returns a Date then \<=\> should work. If it doesn't you'll get a NoMethodError.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hugh

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom1](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Philip\_Hallstrom1](https://discuss.rubyonrails.org/u/Philip_Hallstrom1)\
**Post date:** [September 14, 2007, 5:43pm UTC](https://discuss.rubyonrails.org/t/sorting-different-objects/18162/3 "2007-09-14T17:43:33Z")

</div>

> I have an array of topics and posts each has an updated\_on field. I want to sort the array so in the order of the updated\_on field.
> 
> I.e
> 
> 1 Topic updated on 5 march 2 Poll updated on 4 march 3 Topic updated on 6 march 4 poll updated on 7 march
> 
> shoud go 4,3,1,2
> 
> my code looks like this @topicsAndPolls.sort { |a,b| a.updated\_on \<=\> b.updated\_on} but this isnt working. Do I have to define the \<=\> method somewhere. Can I pass that in the block aswell?

In addition to the other reply, you're looking for a descendant sort... so you really want: { |a,b| b.updated\_on \<=\> a.updated\_on}

(not the switching of a and b)

-philip
