CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 629
Replies: 5 comments · 12 replies
-
You can add this functionality yourself. See This might get you started: local sort_by = function(nodes)
table.sort(nodes, function(a, b)
local a_num = string.match(a.name, "^[0-9]+")
local b_num = string.match(b.name, "^[0-9]+")
if a_num and b_num then
return a_num < b_num
else
-- we need to do something here
return false
end
end)
end |
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm not even a beginner in Lua, but I made this: local compare = function(left, right)
left = left.name:lower()
right = right.name:lower()
for i = 1, string.len(left), 1 do
local l = string.sub(left, i, -1)
local r = string.sub(right, i, -1)
if l == r then
return false
elseif type(tonumber(string.sub(l, 1, 1))) == "number" and type(tonumber(string.sub(r, 1, 1))) == "number" then
return tonumber(string.match(l, "^[0-9]+")) < tonumber(string.match(r, "^[0-9]+"))
elseif l[1] == r[1] then
goto continue
else
return l < r
end
::continue::
end
end
-- nvim-tree.lua
require("nvim-tree").setup({
sort_by = function(nodes)
table.sort(nodes, compare)
end,
})
It seems to work fine. If any one have any optimizations or fail points please tell me. References: https://github.com/lifthrasiir/rust-natord/blob/93e5f0ace208e73ddfaaa77ec1605584c8f22a14/lib.rs#L142 https://github.com/lifthrasiir/rust-natord/blob/93e5f0ace208e73ddfaaa77ec1605584c8f22a14/lib.rs#L37 |
Beta Was this translation helpful? Give feedback.
All reactions
-
Done. |
Beta Was this translation helpful? Give feedback.
All reactions
-
π 1
-
This sort produces inconsistent results for me. After each write of the file nvim-tree re-sorts itself and fails to sort files and directories properly. I am not sure why it happens |
Beta Was this translation helpful? Give feedback.
All reactions
-
π 1
-
I think that this work: local compare = function(left, right)
left = left.name:lower()
right = right.name:lower()
for i = 1, string.len(left), 1 do
local l = string.sub(left, i, -1)
local r = string.sub(right, i, -1)
if l == r then
return false
elseif type(tonumber(string.sub(l, 1, 1))) == "number" and type(tonumber(string.sub(r, 1, 1))) == "number" then
return tonumber(string.match(l, "^[0-9]+")) < tonumber(string.match(r, "^[0-9]+"))
- elseif l[1] == r[1] then
- goto continue
else
return l < r
end
- ::continue::
end
end My subconscious mind told me to get the It appears that this Now, numbers sorting will work only if the number is in the start of the name. For now I'm trying to make it work for number if they are in the middle of end of the name. Sorry about that, but my If you found any problems just say π |
Beta Was this translation helpful? Give feedback.
All reactions
-
This is it, the fixed and improved version: local function natural_cmp(left, right)
left = left.name:lower()
right = right.name:lower()
if left == right then
return false
end
for i = 1, math.max(string.len(left), string.len(right)), 1 do
local l = string.sub(left, i, -1)
local r = string.sub(right, i, -1)
if type(tonumber(string.sub(l, 1, 1))) == "number" and type(tonumber(string.sub(r, 1, 1))) == "number" then
local l_number = tonumber(string.match(l, "^[0-9]+"))
local r_number = tonumber(string.match(r, "^[0-9]+"))
if l_number ~= r_number then
return l_number < r_number
end
elseif string.sub(l, 1, 1) ~= string.sub(r, 1, 1) then
return l < r
end
end
end
require("nvim-tree").setup({
sort_by = function(nodes)
table.sort(nodes, natural_cmp)
end,
}) |
Beta Was this translation helpful? Give feedback.
All reactions
-
π 1
-
Thanks for the update! |
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi. I have a problem. |
Beta Was this translation helpful? Give feedback.
All reactions
-
In your
|
Beta Was this translation helpful? Give feedback.
All reactions
-
I don't understand how to use this parameter with a function from this topic |
Beta Was this translation helpful? Give feedback.
All reactions
-
Extending the example in local sort_by = function(nodes)
table.sort(nodes, function(a, b)
-- directories first
if a.type ~= "directory" and b.type == "directory" then
return false
elseif a.type == "directory" and b.type ~= "directory" then
return true
end
-- sort by name length
return #a.name < #b.name
end)
end |
Beta Was this translation helpful? Give feedback.
All reactions
-
Refactored as per the above example with the comparison function: local natural_cmp = function(left, right)
-- directories first
if left.type ~= "directory" and right.type == "directory" then
--- |
Beta Was this translation helpful? Give feedback.
All reactions
-
π 3
-
I need help again. I'm trying to merge two recipes:
But if I try to add a sorting function to the list for cycling from second reciept, it doesn't work. Cycling is working, but natural sort doesn't. It just sorting by "name".
|
Beta Was this translation helpful? Give feedback.
All reactions
-
A This synthetic example might help: local sort_by_len = true
local sorter = function(nodes)
if sort_by_len then
table.sort(nodes, function(a, b)
return #a.name < #b.name
end)
else
return "modification_time"
end
end
local cycle_sort = function()
sort_by_len = not sort_by_len
api.tree.reload()
end
---
vim.keymap.set('n', 'T', cycle_sort, opts('Cycle Sort')) |
Beta Was this translation helpful? Give feedback.
All reactions
-
π 1
-
Thanks!
|
Beta Was this translation helpful? Give feedback.
All reactions
-
Very nice. You can always add more if the need arises. |
Beta Was this translation helpful? Give feedback.
All reactions
This discussion was converted from issue #1888 on January 07, 2023 03:08.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is your feature request related to a problem? Please describe.
I'm always frustrated when I have a directory that has number-named files.
For example NvimTree will display them like that:
But the normal order I want them to be in is like:
It's the same default behavior of the
ls
GUN utility in Linux, but thels
has an option-v
to treat filename numerically. https://unix.stackexchange.com/questions/33909/list-files-sorted-numericallyMost times I need numbered files sorted with the numerically normal order so I can walk between them easily.
Describe the solution you'd like
Maybe adding a new type to the
sort_by
option to do that type of sorting.Describe alternatives you've considered
Maybe I just can create a function to do that since
sort_by
option support passing a function.Beta Was this translation helpful? Give feedback.
All reactions