[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ordered/sorted hash

robertj

12/8/2005 5:33:00 PM

hi,

is there anywhere a class that does soemthing
akin to java.util.SortedMap? that is sorting
and iterating over the hash in the order of its
keys.

i had a look into facets dictionary but that does
only preserve the ordering of insertion which
is not what i want.

any other suggestions?

ciao robertj

18 Answers

Steve Litt

12/8/2005 6:00:00 PM

0

On Thursday 08 December 2005 12:37 pm, robertj wrote:
> hi,
>
> is there anywhere a class that does soemthing
> akin to java.util.SortedMap? that is sorting
> and iterating over the hash in the order of its
> keys.

When I tried it, it seemed that Ruby automatically sorted the keys. I'm not
sure if this is what you want, but see this:

http://www.troublesh.../codecorn/ruby/basictutorial.h...

HTH

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Bill Kelly

12/8/2005 6:12:00 PM

0

From: "robertj" <robert_kuzelj@yahoo.com>
>
> is there anywhere a class that does soemthing
> akin to java.util.SortedMap? that is sorting
> and iterating over the hash in the order of its
> keys.

Perhaps rbtree?
http://raa.ruby-lang.org/project/ru...


Regards,

Bill




Gene Tani

12/8/2005 6:15:00 PM

0


Steve Litt wrote:
> On Thursday 08 December 2005 12:37 pm, robertj wrote:
> > hi,
> >
> > is there anywhere a class that does soemthing
> > akin to java.util.SortedMap? that is sorting
> > and iterating over the hash in the order of its
> > keys.
>
> When I tried it, it seemed that Ruby automatically sorted the keys. I'm not
> sure if this is what you want, but see this:
>
> http://www.troubleshooters.com/codecorn/ruby/basictutorial.h...
>

Not sure what "it" is, but Hash#sort will convert the hash to a list of
lists, sorted by their keys

Then others have put in rbtrees, arrays accessed by keywords,
http://raa.ruby-lang.org/project/ru...
http://codeforpeople.com/lib/ruby/arrayfields/arrayfields-3....

Berger, Daniel

12/8/2005 6:21:00 PM

0

Gene Tani wrote:
> Steve Litt wrote:
>
>>On Thursday 08 December 2005 12:37 pm, robertj wrote:
>>
>>>hi,
>>>
>>>is there anywhere a class that does soemthing
>>>akin to java.util.SortedMap? that is sorting
>>>and iterating over the hash in the order of its
>>>keys.
>>
>>When I tried it, it seemed that Ruby automatically sorted the keys. I'm not
>>sure if this is what you want, but see this:
>>
>>http://www.troubleshooters.com/codecorn/ruby/basictutorial.h...
>>
>
>
> Not sure what "it" is, but Hash#sort will convert the hash to a list of
> lists, sorted by their keys

True, but unless you need the values stored in order internally, just do this:

hash.sort.each{ |e| puts "#{e[0]} => #{e[1]}" }

Regards,

Dan


robertj

12/8/2005 6:51:00 PM

0

hi,

the only real requirement is
that #each returns me the
key value pairs in an ordered fashion.

default ordering should be by key.

one could but think of also having
a switch that allows for ordering by
value.

ciao robertj

robertj

12/8/2005 6:56:00 PM

0

hi daniel,

>>hash.sort.each{ |e| puts "#{e[0]} => #{e[1]}" }
this has the effect that clients of that hash
need to know that they must order the hash +
the interface for iteration has changed.

instead of hash.each { |k, v| ...} you get hash.sort.each { |v| ...}

in short your solution requires "sorting" to become part of
the "official" interface of my hash.

ciao robertj

robertj

12/8/2005 7:01:00 PM

0

hi T.

another idea would be to be able to define
a comperator block that does the actual
comparisson.

ciao robertj

Trans

12/8/2005 7:06:00 PM

0

Thank robertj I'll work on it now.

By the way have you seen calibre/association? That's kind of neat. It
isn't quite like a regular hash, but you can use it do order hash-like
strucutures.

require 'calibre/association'

assoc_array = [ :a >> 1, :b >> :2, :c >> 3 ]

assoc_array.each{ |k,v| p k,v }

produces

:a
1
:b
2
:c
3

T.

Trans

12/8/2005 7:22:00 PM

0


robertj wrote:
> another idea would be to be able to define
> a comperator block that does the actual
> comparisson.

I see. So you want a way to tell the object itself how it's to order
the elements. The default wprobably should stay insertion order, but
you'd like to specify an alternative like alpahnumeric key order. Is
that right? If so, I can put in an optional parameter for that no
problem. Although you may have to set it post instantiation, something
like

d = Dictionary.new.sort_on { |a,b| a.key <=> b.key }

As a shorthand:

d = Dictionary.new.sort_on(:key)

While I would like to add this as a block/parameter of the initialize
method itself, it may be a problem b/c this should probably be used for
a default block like hash has.

T.

Simon Kröger

12/8/2005 8:16:00 PM

0

robertj wrote:
> hi,
>
> the only real requirement is
> that #each returns me the
> key value pairs in an ordered fashion.
>
> default ordering should be by key.
>
> one could but think of also having
> a switch that allows for ordering by
> value.
>
> ciao robertj

hi,

if speed isn't an issue:

---------------------------------------------
class SortedHash < Hash
alias :unsorted_each :each
def each
keys.sort.each{|k| yield k, self[k]}
end
end

h = SortedHash[*Array.new(20){rand(100)}]
h.each{|k, v| puts "#{k} => #{v}"}
---------------------------------------------

this isn't meant to replace a red black tree
implementation of course.

cheers

Simon