# Dynamic sort\_by criteria, making it DRY

**URL:** <https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017>\
**Category:** Ruby\
**Created:** [June 28, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017 "2007-06-28T13:46:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 28, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017/1 "2007-06-28T13:46:14Z")

</div>

I am presently using a sort\_by on my array

=\> buddies = user\_buddies.sort\_by {|item| item.last\_seen\_at}

I would like to be able to do the same , based on a variable  
@criteria set to the item key to be used

@criteria = ‘last\_seen\_at’ @criteria = ‘display\_name’ …

=\> buddies = user\_buddies.sort\_by {|item| item.\<@criteria\> }

what could be the best way to do it ?

Depending upon another variable @reverse\_order = true or false, I would  
like to perform or not teh reverse! action

=\> buddies = user\_buddies.sort\_by {|item| item.last\_seen\_at}.reverse!

I believe it should be

=\> buddies = @reverse\_order ? user\_buddies.sort\_by {|item|  
item.\<@criteria\> } : user\_buddies.sort\_by {|item| item.\<@criteria\>  
}.reverse!

thanks for your enlightment !

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 28, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017/2 "2007-06-28T14:01:16Z")

</div>

On 28.06.2007 13:41, Josselin wrote:

> item.\<@criteria\> } : user\_buddies.sort\_by {|item| item.\<@criteria\>  
> }.reverse!
> 
> thanks for your enlightment !

buddies = user\_buddies.sort\_by {|it| it.send @criteria}  
buddies.reverse! if @reverse\_order

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 28, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017/3 "2007-06-28T23:21:54Z")

</div>

On 2007-06-28 13:58:47 +0200, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> > =\> buddies = user\_buddies.sort\_by {|item| item.\<@criteria\> }  
> > =\> buddies = @reverse\_order ? user\_buddies.sort\_by {|item|  
> > robert  
> > Thanks Robert cannot be dryest… I am so ashame of my solution that  
> > I don’t want to publish my code…

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 29, 2007, 9:55am UTC](https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017/4 "2007-06-29T09:55:49Z")

</div>

On 29.06.2007 08:12, Josselin wrote:

> > > > I would like to be able to do the same , based on a variable
> > > 
> > > buddies = user\_buddies.sort\_by {|it| it.send @criteria}
> 
> I tried to write it into the @criteria (@criteria =  
> ‘display\_name.downcase’ )  
> which seems to be the right place…
> 
> but I got an error  
> undefined method `display\_name.downcase’ for #User:0x34bc34c
> 
> thanks for your complementary solution… ;-))
> 
> joss

buddies = user\_buddies.sort\_by {|it| it.send(@criteria).downcase}

But this works only if you know that all criteria are strings.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [June 29, 2007, 8:15am UTC](https://www.ruby-forum.com/t/dynamic-sort-by-criteria-making-it-dry/108017/5 "2007-06-29T08:15:39Z")

</div>

On 2007-06-28 23:18:36 +0200, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > > @criteria = ‘last\_seen\_at’ @criteria = ‘display\_name’ …  
> > > I believe it should be  
> > > Kind regards
> > 
> > robert
> 
> Thanks Robert cannot be dryest… I am so ashame of my solution that  
> I don’t want to publish my code…

btw , where should I write the ‘downcase’ method for strings

I tried to write it into the @criteria (@criteria =  
‘display\_name.downcase’ )  
which seems to be the right place…

but I got an error  
undefined method `display\_name.downcase’ for #User:0x34bc34c

thanks for your complementary solution… ;-))

joss
