lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Oct 8, 2016 at 4:55 PM, Mingranina Gingranina
<mingranina@gmail.com> 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