Win32ole object collection mapping to array?

(Peter Fitzgibbons) #1

Hello all,

I have a win32ole object collection that I want to map, then sort, to an
array?

Each item has #Name property. I'm thinking of making an arry of hashes
a'la :

items = []
oleItems.each {|o| items << {o.Name => o} }
items

[{"0172"=>#<WIN32OLE:0x2a88570>}, {"0171"=>#<WIN32OLE:0x2a88540>}]

But then, how do I sort the array of hashes ?

Is there a better approach ?

Peter J. Fitzgibbons
Applications Manager
Lakewood Homes - "The American Dream Builder"(r)
Peter.Fitzgibbons@Lakewoodhomes.net
(847) 884-8800

(Daniel Amelang) #2

Not sure why you'd want an array of hashes, but here goes:

items.sort_by {|h| h.keys[0].to_i}

Maybe I misunderstood. Are planning on later adding multiple objects
with the same Name? Then, you might consider a hash of arrays.

Anyways, why not just make it a simple hash?

hash = {}
oleItems.each {|i| hash[i.Name] = i}
# when you want them in a sorted array
hash.values.sort_by {|v| v.Name.to_i}

Perhaps I need to know more about the context of your code.

Dan

(Brian Takita) #3

Try this...

···

items = []
oleItems.each {|o| items << {o.Name => o} }
items.sort {|x, y| x.Name <=> y.Name}

On 8/25/05, Peter Fitzgibbons <Peter.Fitzgibbons@lakewoodhomes.net> wrote:

Hello all,

I have a win32ole object collection that I want to map, then sort, to an
array?

Each item has #Name property. I'm thinking of making an arry of hashes
a'la :

> items = []
> oleItems.each {|o| items << {o.Name => o} }
> items
[{"0172"=>#<WIN32OLE:0x2a88570>}, {"0171"=>#<WIN32OLE:0x2a88540>}]

But then, how do I sort the array of hashes ?

Is there a better approach ?

Peter J. Fitzgibbons
Applications Manager
Lakewood Homes - "The American Dream Builder"(r)
Peter.Fitzgibbons@Lakewoodhomes.net
(847) 884-8800

(tokmak tokmak) #4

You can try this (apologies if i misunderstood you problem):

oleitems.map{|o| o.Name}.sort

This makes an array of Name values, then sorts it. I couldn't understand if
you want values as an array or hash.

···

On 8/25/05, Peter Fitzgibbons <Peter.Fitzgibbons@lakewoodhomes.net> wrote:

Hello all,

I have a win32ole object collection that I want to map, then sort, to an
array?

Each item has #Name property. I'm thinking of making an arry of hashes
a'la :

> items = []
> oleItems.each {|o| items << {o.Name => o} }
> items
[{"0172"=>#<WIN32OLE:0x2a88570>}, {"0171"=>#<WIN32OLE:0x2a88540>}]

But then, how do I sort the array of hashes ?

Is there a better approach ?

Peter J. Fitzgibbons
Applications Manager
Lakewood Homes - "The American Dream Builder"(r)
Peter.Fitzgibbons@Lakewoodhomes.net
(847) 884-8800

(Brian Takita) #5

Sorry. What I gave you is wrong. Daniel has a good solution.

···

On 8/25/05, Brian Takita <brian.takita@gmail.com> wrote:

Try this...

> items = []
> oleItems.each {|o| items << {o.Name => o} }
> items.sort {|x, y| x.Name <=> y.Name}

On 8/25/05, Peter Fitzgibbons <Peter.Fitzgibbons@lakewoodhomes.net > > wrote:
>
> Hello all,
>
> I have a win32ole object collection that I want to map, then sort, to an
>
> array?
>
> Each item has #Name property. I'm thinking of making an arry of hashes
> a'la :
>
> > items = []
> > oleItems.each {|o| items << {o.Name => o} }
> > items
> [{"0172"=>#<WIN32OLE:0x2a88570>}, {"0171"=>#<WIN32OLE:0x2a88540>}]
>
> But then, how do I sort the array of hashes ?
>
> Is there a better approach ?
>
> Peter J. Fitzgibbons
> Applications Manager
> Lakewood Homes - "The American Dream Builder"(r)
> Peter.Fitzgibbons@Lakewoodhomes.net
> (847) 884-8800
>
>
>
>