# Sort\_by different variables?

**URL:** <https://www.ruby-forum.com/t/sort-by-different-variables/159872>\
**Category:** Ruby\
**Created:** [February 9, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/sort-by-different-variables/159872 "2009-02-09T16:24:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [February 9, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/sort-by-different-variables/159872/1 "2009-02-09T16:24:45Z")

</div>

I have a user control panel that generates an event log by merging  
various things that can happen on the site. These are:

user joins  
user receives invitation  
user completes invitation  
user refers someone  
user referee activates account

So I want to merge these events into a single array and sort them. It  
works well sorting by created\_at. However, I do not have unique classes  
for all of the events I want to merge (yeah, REST would have worked well  
here). So when I populate the array I do this:

```
  # create array of all events for the log
  @events = []
  @events << @user.invitations
  @events << @user.pending_referrals
  @events << @user.active_referrals
  @events = @events.flatten!.sort_by {|x| x.created_at }.reverse

```

A pending\_referral and active\_referral are the same object,but pending  
has an activated\_at = nil and an active\_referral has activated\_at =  
datetime. So, it is possible to sort this array by created\_at OR  
activated\_at?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [February 9, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/sort-by-different-variables/159872/2 "2009-02-09T17:52:11Z")

</div>

2009/2/9 Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> works well sorting by created\_at. However, I do not have unique classes  
> for all of the events I want to merge (yeah, REST would have worked well  
> here). So when I populate the array I do this:
> 
> ```
> # create array of all events for the log
> @events = []
> @events << @user.invitations
> @events << @user.pending_referrals
> @events << @user.active_referrals
> @events = @events.flatten!.sort_by {|x| x.created_at }.reverse
> 
> ```

First of all, I believe you want #concat and not #\<\<:

irb(main):003:0\> a = []  
=\> []  
irb(main):004:0\> a \<\< [1,2,3]  
=\> [[1, 2, 3]]  
irb(main):005:0\> a \<\< [6,7,8]  
=\> [[1, 2, 3], [6, 7, 8]]  
irb(main):006:0\> a  
=\> [[1, 2, 3], [6, 7, 8]]

irb(main):007:0\> a = []  
=\> []  
irb(main):008:0\> a.concat [1,2,3]  
=\> [1, 2, 3]  
irb(main):009:0\> a.concat [6,7,8]  
=\> [1, 2, 3, 6, 7, 8]  
irb(main):010:0\> a  
=\> [1, 2, 3, 6, 7, 8]

> A pending\_referral and active\_referral are the same object,but pending  
> has an activated\_at = nil and an active\_referral has activated\_at =  
> datetime. So, it is possible to sort this array by created\_at OR  
> activated\_at?

arr.sort\_by {|x| x.activated\_at || x.created\_at}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [February 9, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/sort-by-different-variables/159872/3 "2009-02-09T17:53:54Z")

</div>

Thanks a lot, Robert!
