[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Grouping/Sorting an Array of objects

Howard Roberts

7/21/2008 3:24:00 AM

Suppose I have an array of two different kinds of objects, each with
different attributes, but both having a date attribute. I'd like to be
able to loop through the array in such a manner as to be able print a
result similar to the following:


07/19/08
Foo-1 - Name
attr 1
attr 2
[..]

Foo-2 - Name
attr 1
attr 2
[..]

Bar - Name
attr 1
attr 2
[..]
07/20/08
Foo-1 - Name
attr 1
attr 2
[..]

Bar - Name
attr 1
attr 2
[..]
etc.

showing all of the Foo's first, then the Bar's, for each date.
If I only have Foo's, I can easily do:

@data.sort{|x,y| Time.parse(x.date) <=> Time.parse(y.date)}).each do
|foo|
puts Foo.date # Still shows date on each loop, but okay
puts Foo.name
[..]
end

To get things in date order, but when I consider mixing a second object
into the array, that confounds matters for me. I suspect group_by{|item|
item.class} may be part of the solution, but I'm not sure how?

Any pointers would be most appreciated.

Thanks,
Howard
--
Posted via http://www.ruby-....

6 Answers

Peña, Botp

7/21/2008 3:58:00 AM

0

RnJvbTogSG93YXJkIFJvYmVydHMgW21haWx0bzpob3dhcmRyb2JlcnRzQGNvbWNhc3QubmV0XSAN
CiMuLi4NCiMgQGRhdGEuc29ydHt8eCx5fCBUaW1lLnBhcnNlKHguZGF0ZSkgPD0+IFRpbWUucGFy
c2UoeS5kYXRlKX0pLmVhY2ggZG8NCiMgfGZvb3wNCiMgICBwdXRzIEZvby5kYXRlICMgU3RpbGwg
c2hvd3MgZGF0ZSBvbiBlYWNoIGxvb3AsIGJ1dCBva2F5DQojICAgcHV0cyBGb28ubmFtZQ0KIyAg
IFsuLl0NCiMgZW5kDQoNCnRyeSwNCg0KICBAZGF0YS5zb3J0X2J5e3xyZWN8IFtyZWMuZGF0ZSwg
cmVjLmNsYXNzLCByZWMubmFtZV19DQoNCm9yIHNvbWV0aGluZyBsaWtlIHRoYXQuDQoNCml0IHdv
dWxkIGJlIG5pY2UgaWYgeW91IGNhbiBwb3N0IHNvbWUgc2FtcGxlIGRhdGEgc28gd2UgY2FuIHRl
c3QgaXQgaW1tZWRpYXRlbHkuIGknbSBhIHNsb3cgdHlwaXN0IDspDQoNCg0KIyBUbyBnZXQgdGhp
bmdzIGluIGRhdGUgb3JkZXIsIGJ1dCB3aGVuIEkgY29uc2lkZXIgbWl4aW5nIGEgDQojIHNlY29u
ZCBvYmplY3QgaW50byB0aGUgYXJyYXksIHRoYXQgY29uZm91bmRzIG1hdHRlcnMgZm9yIG1lLiAN
CiMgSSBzdXNwZWN0IGdyb3VwX2J5e3xpdGVtfCBpdGVtLmNsYXNzfSBtYXkgYmUgcGFydCBvZiB0
aGUgc29sdXRpb24sIA0KIyBidXQgSSdtIG5vdCBzdXJlIGhvdz8NCg0KdGhhdCBpcyBwb3NzaWJs
ZSB0b28sIGJ1dCBsZXQgdXMgdGFrZSBpdCBvbmUgYXQgYSB0aW1lIDspDQoNCmtpbmQgcmVnYXJk
cyAtYm90cA0K

Howard Roberts

7/21/2008 7:09:00 PM

0

Peña, Botp wrote:

> try,
>
> @data.sort_by{|rec| [rec.date, rec.class, rec.name]}
>
> or something like that.

Something like that is probably what I need, because the app doesn't
like that line for me.
> it would be nice if you can post some sample data so we can test it
> immediately. i'm a slow typist ;)
some sample data and pseudo code is here:
http://ruby.pastebin.co...

The output I'm trying to achieve would look like the following, listing
first the date, then the information all of the first objects , and then
all of the second objects for that date:

7/18/08

9:00 AM - Orientation
Location: Conference Room
Welcome, review agenda, and brief Q&A

7:30 AM - First Event
The Smiths invite you for a casual breakfast at Grumpy's

7/19/08
8:00 AM - Workshop I
Location: Room 106
Boring, all day presentation by Dr. Jones


7/20/08
6:00 PM - Second Event
The Jones invite you for a Sociable Dinner at La Nepolara's

7/21/08
8:00 AM - Workshop II
Location: Room 108
Yet another boring, all day presentation by Dr. Jones

2:30 PM - Wrap-Up
Location: Room 108
Summary, Q&A, and lots of free goodies

7:30 AM - Third Event
The Smiths invite you for a casual Breakfast at Mel's

1:00 PM - Fourth Event
Dr. Jones and Staff invite you for a Business Lunch at The Krusty Krab


> kind regards -botp
Thank you kindly for replying and a for any suggestions you might offer.

Howard

--
Posted via http://www.ruby-....

Howard Roberts

7/22/2008 10:32:00 AM

0

Peña, Botp wrote:
> prev_date=nil
> @data.sort_by do |item|
> t,ampm = item.time.split
> hr,min = t.split ":"
> hr = "0"+hr if hr.size==1
> timenew = ampm+hr+min
> [item.date, timenew, item.class.to_s]
> end.each do |item|

Brilliant!

> There's some clobbering of time in there since i arranged by time too :)
I can adapt my app for that.

> Is that ok?
> Kind regards -botp

-botp,
This is superb! I was reluctant to ask for help on this at all because
it sounded so much like I was asking "write my code for me." But I would
have never gotten that, because I did not know you could use sort_by in
such a way-- passing an array as the last line of the block. It is
exactly what I needed and I am most grateful-- for the clearer
understanding (and the code :)

Thank You,
Howard



--
Posted via http://www.ruby-....

David A. Black

7/22/2008 11:22:00 AM

0

Hi --

On Mon, 21 Jul 2008, Howard Roberts wrote:

> Suppose I have an array of two different kinds of objects, each with
> different attributes, but both having a date attribute. I'd like to be
> able to loop through the array in such a manner as to be able print a
> result similar to the following:
>
>
> 07/19/08
> Foo-1 - Name
> attr 1
> attr 2
> [..]
>
> Foo-2 - Name
> attr 1
> attr 2
> [..]
>
> Bar - Name
> attr 1
> attr 2
> [..]
> 07/20/08
> Foo-1 - Name
> attr 1
> attr 2
> [..]
>
> Bar - Name
> attr 1
> attr 2
> [..]
> etc.
>
> showing all of the Foo's first, then the Bar's, for each date.
> If I only have Foo's, I can easily do:
>
> @data.sort{|x,y| Time.parse(x.date) <=> Time.parse(y.date)}).each do
> |foo|
> puts Foo.date # Still shows date on each loop, but okay
> puts Foo.name
> [..]
> end
>
> To get things in date order, but when I consider mixing a second object
> into the array, that confounds matters for me. I suspect group_by{|item|
> item.class} may be part of the solution, but I'm not sure how?
>
> Any pointers would be most appreciated.

Here's a stab at it: http://pastie..... I've put the sorting
intelligence in the objects, and also given them to_s methods so you
don't have to do all that formatting in the calling code (though you
can, of course, if you want a different output).

The last ten lines still seem a bit wordy to me but so be it; I have
to go get ready for work :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!

Erik Veenstra

7/22/2008 2:56:00 PM

0

a.group_by do |o|
o.date
end.sort.each do |date, list|
p date

list.group_by do |o|
o.class
end.sort_by do |klass, _|
[Foo, Bar].index(klass) # kind_of?
end.each do |klass, list|
list.each do |o|
p o
end
end
end

gegroet,
Erik V. - http://www.erikve...

PL

9/13/2011 12:39:00 PM

0

On 13/09/2011 8:02, CubaFAQ wrote:

(snip)
> By using my full name here, the cowardly blackmailer, [abuse of my
privacy by blackmailer Dan Christensen],

You might just as well stop your desperate attempts to blackmail me in
to stopping with posting with your violations of my privacy Dan
Christensen (aka DCPROOF)

I will just continue to reply to you using the alias you selected and
have used for twelve years and even up to the last couple of days (your
choice).

"Blackmailer **** **** (PL) up to his old tricks

From: Dan Christensen <Dan_Christensen@xxxxxxxxxxxx>
Date: Wed, 24 Aug 2011 07:21:06 -0700 (PDT)"

http://newsgroups.derkeiler.com/Archive/Soc/soc.culture.cuba/2011-08/msg...

""Ladies" want violent thugs, terrorists and spies released to continue
US dirty work?

From: Dan Christensen <Dan_Christensen@xxxxxxxxxxxx>
Date: Tue, 23 Aug 2011 23:23:39 -0700 (PDT)"

http://newsgroups.derkeiler.com/Archive/Soc/soc.culture.cuba/2011-08/msg...


Thanks for exposing to all again that you do use all of your aliases
("Dan Christensen", "DCPROOF", "CUBAFAQ") in your slander and blackmail
campaign.
Thanks also for exposing yourself as the pathetic liar you are by again
showing that you do continue to post with "Dan Christensen" in your
personal attacks

As I said:

You are the only one attempting - and failing to - blackmail anyone Dan
Christensen (aka DCPROOF).

I merely use the alias you selected 12 years ago and that you have also
used with this e-mail.
https://groups.google.com/group/soc.culture.cuba/search?hl=en&q=pl+author%3ADan+Christensen&start=0&scoring=d&...

An alias you have used even today:
http://newsgroups.derkeiler.com/Archive/Soc/soc.culture.cuba/2011-08/msg...

The facts show that you are the only blackmailer.

Link:
https://groups.google.com/group/soc.culture.cuba/browse_thread/thread/fad55c8147d334eb/012d5390e11a93c1?hl=en%0Cd53...

The two elements of blackmail are here:
- you tell me to stop referring to the alias I used for the last 10 years
OR
- you will post a constant stream of slanderous personal attacks to
defame you on the web.

As long as you continue this abuse I will expose you fro the vulgar
lying hypocrite of a blackmailer you are.

When you have had enough of your own abuse and its consequences then
just stop abusing.


You started this, you end it, but be sure of one thin: your blackmail
won't work now just like it hasn't worked it the past.

To paraphrase you: "you are the instigator of this abuse,so it is
entirely up to you to put an end to this".

In the mean time I will keep your score and share it with all.