# Sorting by timestamp with uneven arrays

**URL:** <https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865>\
**Category:** ruby-talk\
**Created:** [8 December 2007 22:03 UTC](https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865 "2007-12-08T22:03:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Dershowitz](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mike\_Dershowitz](https://rubytalk.org/u/Mike_Dershowitz)\
**Post date:** [8 December 2007 22:03 UTC](https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865/1 "2007-12-08T22:03:52Z")

</div>

Ok, this one is a little complicated and I'm sure there's a better way  
to do it. Here's what I'm doing:

For presentation purposes, I have an array of arrays. Here's the  
structure of the array:

[  
"indicator (string that I set" ,  
\<model object from db, different models\> ,  
timestamp  
]

I'm creating and inserting the timestamp myself, as each model object  
has a form of timestamp, whether or not it's called that.

I'm doing it this way becuase I use a model method to get all of this  
data, and the thing I want to do last is to sort by "timestamp".

array.sort\_by won't work becuase it's not a hash (although i tried  
adding a hash and then sorting by that hash, but that didn't work either  
(got a nomethod error).

How can I make it so that I can sort this array?

Thanks so much in advance!

Mike

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Chris\_Shea1](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@Chris\_Shea1](https://rubytalk.org/u/Chris_Shea1)\
**Post date:** [8 December 2007 22:50 UTC](https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865/2 "2007-12-08T22:50:12Z")

</div>

You have an array of arrays, each of which has as its last element a  
timestamp? You should just be able to do this: array.sort\_by{|e|  
e.last}

Or did I read this wrong?

Chris

> **···**
>
> On Dec 8, 3:03 pm, Mike Dershowitz \<michael.dershow...@jpmchase.com\> wrote:
> 
> > Ok, this one is a little complicated and I'm sure there's a better way  
> > to do it. Here's what I'm doing:
> > 
> > For presentation purposes, I have an array of arrays. Here's the  
> > structure of the array:
> > 
> > [  
> > "indicator (string that I set" ,  
> > \<model object from db, different models\> ,  
> > timestamp  
> > ]
> > 
> > I'm creating and inserting the timestamp myself, as each model object  
> > has a form of timestamp, whether or not it's called that.
> > 
> > I'm doing it this way becuase I use a model method to get all of this  
> > data, and the thing I want to do last is to sort by "timestamp".
> > 
> > array.sort\_by won't work becuase it's not a hash (although i tried  
> > adding a hash and then sorting by that hash, but that didn't work either  
> > (got a nomethod error).
> > 
> > How can I make it so that I can sort this array?
> > 
> > Thanks so much in advance!
> > 
> > Mike  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [8 December 2007 22:52 UTC](https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865/3 "2007-12-08T22:52:14Z")

</div>

Hi --

> **···**
>
> On Sun, 9 Dec 2007, Mike Dershowitz wrote:
> 
> > Ok, this one is a little complicated and I'm sure there's a better way  
> > to do it. Here's what I'm doing:
> > 
> > For presentation purposes, I have an array of arrays. Here's the  
> > structure of the array:
> > 
> > [  
> > "indicator (string that I set" ,  
> > \<model object from db, different models\> ,  
> > timestamp  
> > ]
> > 
> > I'm creating and inserting the timestamp myself, as each model object  
> > has a form of timestamp, whether or not it's called that.
> > 
> > I'm doing it this way becuase I use a model method to get all of this  
> > data, and the thing I want to do last is to sort by "timestamp".
> > 
> > array.sort\_by won't work becuase it's not a hash (although i tried  
> > adding a hash and then sorting by that hash, but that didn't work either  
> > (got a nomethod error).
> > 
> > How can I make it so that I can sort this array?
> > 
> > Thanks so much in advance!
> 
> I'm not sure what you mean about #sort\_by and being a hash. You can  
> use it on arrays.
> 
> Can you do this:
> 
> &nbsp;&nbsp;&nbsp;array\_of\_arrays.sort\_by {|array| array[-1] }
> 
> assuming timestamp is always the last thing in the array?
> 
> David
> 
> --  
> Training for 2008!  
> Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Intro to Rails, New York, NY, February 4-7 2008  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Advancing With Rails, New York, NY, February 11-14 2008  
> Hosted by Exceed Education. See [http://www.rubypal.com](http://www.rubypal.com) for details!

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [8 December 2007 23:11 UTC](https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865/4 "2007-12-08T23:11:06Z")

</div>

I'm not sure why you are looking for a hash.

data = [  
["model1", "instance of 1", Time.now],  
["model2", "instance of 2", Time.now - 100]  
]

p data.sort\_by { |x| x[2] } # =\> [["model2", "instance of 2", Sat Dec 08 17:15:41 -0500 2007], ["model1", "instance of 1", Sat Dec 08 17:17:21 -0500 2007]]

Gary Wright

> **···**
>
> On Dec 8, 2007, at 5:03 PM, Mike Dershowitz wrote:
> 
> > array.sort\_by won't work becuase it's not a hash (although i tried  
> > adding a hash and then sorting by that hash, but that didn't work either  
> > (got a nomethod error).

---

<div class="post-metadata">

**Author:** ![Mike\_Dershowitz](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mike\_Dershowitz](https://rubytalk.org/u/Mike_Dershowitz)\
**Post date:** [9 December 2007 19:32 UTC](https://rubytalk.org/t/sorting-by-timestamp-with-uneven-arrays/42865/5 "2007-12-09T19:32:02Z")

</div>

thanks everybody, it worked!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
