Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ordered/sorted hash

Reply
Thread Tools

ordered/sorted hash

 
 
robertj
Guest
Posts: n/a
 
      12-08-2005
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

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      12-08-2005
Hi Robert,

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


The dictionary class in Facets/Calibre is basically the ordered hash
written by jan molic. I've been wanting to improve on it. This looks
like the oppornunity. What is it that you need? I'd be happy to work in
your critera.

Thanks,
T.

 
Reply With Quote
 
 
 
 
Steve Litt
Guest
Posts: n/a
 
      12-08-2005
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/codec...al.htm#_Hashes

HTH

SteveT

Steve Litt
http://www.troubleshooters.com
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Bill Kelly
Guest
Posts: n/a
 
      12-08-2005
From: "robertj" <(E-Mail Removed)>
>
> 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/ruby-rbtree/


Regards,

Bill




 
Reply With Quote
 
Gene Tani
Guest
Posts: n/a
 
      12-08-2005

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/codec...al.htm#_Hashes
>


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/ruby-rbtree/
http://codeforpeople.com/lib/ruby/ar...s-3.5.0/README

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      12-08-2005
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/codec...al.htm#_Hashes
>>

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


 
Reply With Quote
 
robertj
Guest
Posts: n/a
 
      12-08-2005
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

 
Reply With Quote
 
robertj
Guest
Posts: n/a
 
      12-08-2005
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

 
Reply With Quote
 
robertj
Guest
Posts: n/a
 
      12-08-2005
hi T.

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

ciao robertj

 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      12-08-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Is there a hash algorithm with direct access to hash elements andreference count? Bo Peng C++ 4 03-12-2006 05:57 AM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments