我将如何将目录排序到列表顶部,而无需在 Lua 中更改列表(现在按字母顺序排序)


if command == "dir" then
local filelist = fs.list("")
for _, file in pairs(filelist) do
if fs.isDir(file) == true then
term.setTextColor(8)
print(file)
term.setTextColor(colors.yellow)
else
term.setTextColor(1)
print(file.." "..fs.getSize(file))
end
end
end

这是我的代码,它会遍历每个项目,然后打印该项目,如果它是一个文件,它会打印文件的大小,并根据它是否是目录更改文本颜色,但我也希望目录位于顶部,但我不知道该怎么做,所以我需要一些帮助

if command == "dir" then
local filelist = fs.list("")
for _, file in pairs(filelist) do
if fs.isDir(file) == true then
term.setTextColor(8)
print("- "..file)
term.setTextColor(colors.yellow)
end
end
for _, file in pairs(filelist) do
if fs.isDir(file) == false then
term.setTextColor(2)
print("- "..file.." "..fs.getSize(file).." bytes")
term.setTextColor(colors.yellow)
end
end
end

我成功了

最新更新