Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hash Sorting

Reply
Thread Tools

Hash Sorting

 
 
Nico Ritsche
Guest
Posts: n/a
 
      01-30-2008
Gary Wright wrote:
> On Jan 30, 2008, at 1:39 AM, Nico Ritsche wrote:
>> all.

> Can you cut, paste, and post the snippit of code you are using to
> display your incorrect results?


I diplay the result like this (in a rails rhtml.file, so there are
actually <% %> around the statements):

pi_array_sorted.each do |a|
a[0].titleize

... some other code ...

end

Well, maybe I need to strip down the example a bit, and test it with
simpler hashs without nested arrays as values. I suspect that the nested
arrays could be the problem...

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

 
Reply With Quote
 
 
 
 
Ilan Berci
Guest
Posts: n/a
 
      01-30-2008

>
> But this is not what I get. The keys in the resulting array still have
> an arbitrary order, the exact same order as if I don't call .sort at
> all.
>
> Nico


That is what I assumed, check out my earlier solution and that will work
for you.. It will convert a hash of unsorted arrays to a sorted array of
sorted arrays..

hth

ilan


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

 
Reply With Quote
 
 
 
 
Nico Ritsche
Guest
Posts: n/a
 
      01-30-2008
Ilan Berci wrote:
>
>>
>> But this is not what I get. The keys in the resulting array still have
>> an arbitrary order, the exact same order as if I don't call .sort at
>> all.
>>
>> Nico

>
> That is what I assumed, check out my earlier solution and that will work
> for you.. It will convert a hash of unsorted arrays to a sorted array of
> sorted arrays..
>
> hth
>
> ilan



You mean this one?

> irb(main):002:0> a = {1=>[3,7,1,7], 2=>[3,7,1,1],3=>[3,2,2,9]}
> => {1=>[3, 7, 1, 7], 2=>[3, 7, 1, 1], 3=>[3, 2, 2, 9]}
> irb(main):003:0> a.each {|b,c| c.sort!}
> => {1=>[1, 3, 7, 7], 2=>[1, 1, 3, 7], 3=>[2, 2, 3, 9]}
> irb(main):004:0> a.sort
> => [[1, [1, 3, 7, 7]], [2, [1, 1, 3, 7]], [3, [2, 2, 3, 9]]]


Well, as I said my focus is on sorting the keys, not the nested arrays,
although I might need to do that as well later. But this solution
doesn't work
in my case, as I pointed out. I call hash.sort, directly, which is
equivilant to omiting your nested array search (a.each {|b,c| c.sort!})
which doesn't have anything to do with sorting the keys anyway, it only
sorts the values, i.e. the nested arrays. a.sort doesn't sort the keys
in my example for some reason.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jon Egil Stand
Guest
Posts: n/a
 
      01-30-2008
If you have a Hash like this
{1=>5, 19=>4, 9=>88}

>> h.keys.sort

=> [1, 9, 19]

sorts the keys.


All the best
Jon Egil

 
Reply With Quote
 
Nico Ritsche
Guest
Posts: n/a
 
      01-31-2008
Jon Egil Stand wrote:
> If you have a Hash like this
> {1=>5, 19=>4, 9=>88}
>
>>> h.keys.sort

> => [1, 9, 19]
>
> sorts the keys.
>
>
> All the best
> Jon Egil


Yipee, this finally works! Thanks Jon!

I simply had to sort the keys and the iterate ofer the sorted keys,
using the keys for indexing into the hash:

@sorted_keys = @parent_items.keys.sort

then

@sorted_keys.each do |key|
@parent_items[key].do_whatever
key.do_whatever
end

Easy, but still wonder why the other solutions don't work...
--
Posted via http://www.ruby-forum.com/.

 
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
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Sorting a hash containing a hash of hashes IanW Perl Misc 3 12-14-2005 03:02 PM
[Nuby] Sorting a Hash and keepint it as a Hash? Williams, Chris Ruby 3 12-13-2004 09:04 PM



Advertisments