sorting by timestamp with uneven arrays

  • Thread starter Mike Dershowitz
  • Start date
M

Mike Dershowitz

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
 
C

Chris Shea

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

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
 
D

David A. Black

Hi --

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:

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:
* Intro to Rails, New York, NY, February 4-7 2008
* Advancing With Rails, New York, NY, February 11-14 2008
Hosted by Exceed Education. See http://www.rubypal.com for details!
 
G

Gary Wright

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).

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top