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