Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > sort_by and directories

Reply
Thread Tools

sort_by and directories

 
 
Dafydd Fontenot
Guest
Posts: n/a
 
      04-28-2009
On my system I have this small ruby script as a test to arrange files.
I'm using "find" and trying to get the files sorted starting with the
files the farthest away from the base folder first in the array (so the
files nested in the most sub directories are first on the array).

Here's what the script looks like
test = Array.new

`find /folder/place -type f`.split("\n").each do |file|
test.push(file)
end

test.sort_by { |file| file.gsub(/[^\/]/, "").length }
puts test

However when I run the script the files are not actually organized any
different than what find returns.

Can someone correct me on my methodology?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      04-28-2009
Hi,

At Tue, 28 Apr 2009 11:42:07 +0900,
Dafydd Fontenot wrote in [ruby-talk:335235]:
> Here's what the script looks like
> test = Array.new
>
> `find /folder/place -type f`.split("\n").each do |file|
> test.push(file)
> end
>
> test.sort_by { |file| file.gsub(/[^\/]/, "").length }
> puts test


Array#sort_by returns sorted new array, but doesn't change the
receiver itself.

require 'find'
test = []
Find.find("/folder/place") {|file| test.push(file) if File.file?(file)}
puts test.sort_by {|file| file.count("/")}

--
Nobu Nakada

 
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
#sort_by and #sort_obj Eric Hodel Ruby 17 10-08-2007 06:04 PM
sort_by{rand} doesn't shuffle array Pat Maddox Ruby 4 01-03-2006 12:12 PM
stable sort_by? Patrick Gundlach Ruby 11 12-14-2005 06:03 PM
Extracting Directories and Sub Directories and Counting Ron Smith Perl Misc 5 11-02-2004 11:23 PM
A sort_by descending sort Michael Gaunnac Ruby 8 10-09-2004 03:47 PM



Advertisments