From: Arlen Cuss Date: 2008-04-26T07:49:17+09:00 Subject: Re: pretty folder tree script ------=_Part_8752_8606500.1209163755782 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, On Sat, Apr 26, 2008 at 5:45 AM, John wrote: > So now it looks like (ascii rawx!!1) > > .------------------- > | folder A title > | .------------------ > | | folder B title > | `----------------- > | child_file_of_A > | .------------------- > | | folder C > | | child_file_of_C > | `------------------- > `----------------- > > example.sort.each just smashes the whole thing to bits. What the F do > I do now? I think Eivind's solution definitely has some merit, perhaps it's just a simple bug that needs to be fixed. I'll look at it tonight. For this problem, you can't sort `example' - you need to work on the data after it's been collected into the hashes. I don't have the time to implement it right now, but basically, as we look at each directory, we convert the hash we'd do the `blah.each' on into an Array instead: >> {:q => 2, :c => 4}.to_a => [[:c, 4], [:q, 2]] >> Then we can sort this array in whatever way we like: >> {:q => 2, :c => 4}.to_a.sort_by {|a| a[1]} => [[:q, 2], [:c, 4]] >> Notice in this example I `sorted by' a[1] for every element - that is, it used the 1th item of each element as the index. (in this case, the numbers 2 and 4) If I said `a[0]', it would've tried to use the symbols (and you can't sort by symbol). If we wanted them in the order `:c', `:q', we could order by a[0].to_s, if we like: >> {:q => 2, :c => 4}.to_a.sort_by {|a| a[0].to_s} => [[:c, 4], [:q, 2]] >> This is just an example of how you could do it. Maybe you can work it out from here. If not, I'll be back tonight and I'll take a look at Eivind's method and how you can sort properly with that, too. Cheers, Arlen ------=_Part_8752_8606500.1209163755782--