-
Dear community, my goal is to sort files and directories by modification time. The documentation provides an example for a custom function:
How can I adapt this function? Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
That is going to be real tricky because it does not currently collect the modified time when it scan the filesystem, just the name and the type. To make this work you would need to lookup the modification time by yourself. That would of course be crazy inefficient in a sort function so you would need to create a lookup table outside of that function so that it is only looked up once per node. You would then need to take care to invalidate that cache. I would probably have the lookup be something like this: local modified_lookup = {
["my/file/name.txt"] = {
modified = 1692279525, -- unix epoch time
lookup_time = 1692279525 -- last time it was read from the file system
}
}
local function get_current_time()
-- implement this
end
local function get_modified_time()
-- implement this
end
local function get_modified(filename)
local val = modified_lookup[filename]
local lookup_time = val and val.lookup_time or 0
local now = get_current_time()
local age = now - lookup_time
if age > 1000 then
val = nil
end
if not val then
-- get the modified time
modified = get_modified_time(filename)
val = {
modified = modified
lookup_time = now
}
modified_lookup[filename] = val
end
return val.modified
end
local sort_function = function (a,b)
local a_mod = get_modified(a.path)
local b_mod = get_modified(b.path)
if a.type == b.type then
return a_mod > b_mod
else
return a.type > b.type
end
end |
Beta Was this translation helpful? Give feedback.
-
This discussion has inspired me to start collecting AND showing these extra stats. It's still a WIP but see here: #1107 The next logical step would be to add the ability to sort on any column. |
Beta Was this translation helpful? Give feedback.
-
This functionality was just merged to |
Beta Was this translation helpful? Give feedback.
This functionality was just merged to
main
@tmummert. Useom
to sort by last modified.