How to sort the output of lfs.dir

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to sort the output of lfs.dir

Mingranina Gingranina
 Dear List,
 Hello,

 In the following function defined in Fontsampler for quick overview
of all fonts in a directory hierarchy, I need the output to be sorted
 alphabetically. How can I do that?

 function dirtree(dir)
  assert(dir and dir ~= "", "directory parameter is missing or empty")
  if string.sub(dir, -1) == "/" then
    dir=string.sub(dir, 1, -2)
  end

  local function yieldtree(dir)
    for entry in lfs.dir(dir) do
      if not entry:match("^%.") then
        entry=dir.."/"..entry
       if not lfs.isdir(entry) then
         coroutine.yield(entry,lfs.attributes(entry))
       end
       if lfs.isdir(entry) then
         yieldtree(entry)
       end
      end
    end
  end

  return coroutine.wrap(function() yieldtree(dir) end)
end

 Any help would be gratefully appreciated.

 Thanks,
 Mingranina

Reply | Threaded
Open this post in threaded view
|

Re: How to sort the output of lfs.dir

Nagaev Boris
On Sat, Oct 8, 2016 at 4:55 PM, Mingranina Gingranina
<[hidden email]> wrote:

>  Dear List,
>  Hello,
>
>  In the following function defined in Fontsampler for quick overview
> of all fonts in a directory hierarchy, I need the output to be sorted
>  alphabetically. How can I do that?
>
>  function dirtree(dir)
>   assert(dir and dir ~= "", "directory parameter is missing or empty")
>   if string.sub(dir, -1) == "/" then
>     dir=string.sub(dir, 1, -2)
>   end
>
>   local function yieldtree(dir)
>     for entry in lfs.dir(dir) do
>       if not entry:match("^%.") then
>         entry=dir.."/"..entry
>           if not lfs.isdir(entry) then
>             coroutine.yield(entry,lfs.attributes(entry))
>           end
>           if lfs.isdir(entry) then
>             yieldtree(entry)
>           end
>       end
>     end
>   end
>
>   return coroutine.wrap(function() yieldtree(dir) end)
> end
>
>  Any help would be gratefully appreciated.
>
>  Thanks,
>  Mingranina
>


Hello Mingranina,

This function returns an iterator. You should create an empty table,
fill it with all elements from the iterator and then sort the table.

local items = {}
for item in dirtree(dir) do
  table.insert(items, item)
end
table.sort(items)


--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: How to sort the output of lfs.dir

Rena
In reply to this post by Mingranina Gingranina
On Sat, Oct 8, 2016 at 9:55 AM, Mingranina Gingranina <[hidden email]> wrote:
 Dear List,
 Hello,

 In the following function defined in Fontsampler for quick overview
of all fonts in a directory hierarchy, I need the output to be sorted
 alphabetically. How can I do that?

 function dirtree(dir)
  assert(dir and dir ~= "", "directory parameter is missing or empty")
  if string.sub(dir, -1) == "/" then
    dir=string.sub(dir, 1, -2)
  end

  local function yieldtree(dir)
    for entry in lfs.dir(dir) do
      if not entry:match("^%.") then
        entry=dir.."/"..entry
          if not lfs.isdir(entry) then
            coroutine.yield(entry,lfs.attributes(entry))
          end
          if lfs.isdir(entry) then
            yieldtree(entry)
          end
      end
    end
  end

  return coroutine.wrap(function() yieldtree(dir) end)
end

 Any help would be gratefully appreciated.

 Thanks,
 Mingranina


Add the files to a table, then use table.sort().

--
Sent from my Game Boy.
Reply | Threaded
Open this post in threaded view
|

Re: How to sort the output of lfs.dir

Mingranina Gingranina
 Dear Boris Nagaev and Rena,
 Hello,

 Many thank for your reply. That solved my problem.

 Yours,
 Mingranina

On 10/8/16, Rena <[hidden email]> wrote:

> On Sat, Oct 8, 2016 at 9:55 AM, Mingranina Gingranina
> <[hidden email]>
> wrote:
>
>>  Dear List,
>>  Hello,
>>
>>  In the following function defined in Fontsampler for quick overview
>> of all fonts in a directory hierarchy, I need the output to be sorted
>>  alphabetically. How can I do that?
>>
>>  function dirtree(dir)
>>   assert(dir and dir ~= "", "directory parameter is missing or empty")
>>   if string.sub(dir, -1) == "/" then
>>     dir=string.sub(dir, 1, -2)
>>   end
>>
>>   local function yieldtree(dir)
>>     for entry in lfs.dir(dir) do
>>       if not entry:match("^%.") then
>>         entry=dir.."/"..entry
>>           if not lfs.isdir(entry) then
>>             coroutine.yield(entry,lfs.attributes(entry))
>>           end
>>           if lfs.isdir(entry) then
>>             yieldtree(entry)
>>           end
>>       end
>>     end
>>   end
>>
>>   return coroutine.wrap(function() yieldtree(dir) end)
>> end
>>
>>  Any help would be gratefully appreciated.
>>
>>  Thanks,
>>  Mingranina
>>
>>
> Add the files to a table, then use table.sort().
>
> --
> Sent from my Game Boy.
>