Skip to content

Commit

Permalink
Sort entries with natural sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaseras committed Mar 10, 2024
1 parent 18dfd24 commit 4176bc7
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,32 @@ M.initialize = function(bufnr)
keymap_util.set_keymaps(config.keymaps, bufnr)
end

--- Compare two strings while also treating multi-digit integers atomically
---@param a_val string
---@param b_val string
---@param order string
---@return boolean
local function natural_sort_compare(a_val, b_val, order)
---This method pads integers with zeroes so that they are always 12 digits long.
---As a result, comparing strings that have integers of different sizes work
---as if we were comparing integers (instead of strings of integers)
---
---Note: if the file name has integers of more than 12 digits, this padding does not work
---@param int integer
local function pad_to_compare(int)
return string.format("%012d", int)
end

local a_val_padded = string.gsub(a_val, "%d+", pad_to_compare)
local b_val_padded = string.gsub(b_val, "%d+", pad_to_compare)

if order == "desc" then
return a_val_padded > b_val_padded
else
return a_val_padded < b_val_padded
end
end

---@param adapter oil.Adapter
---@return fun(a: oil.InternalEntry, b: oil.InternalEntry): boolean
local function get_sort_function(adapter)
Expand Down Expand Up @@ -547,11 +573,7 @@ local function get_sort_function(adapter)
local a_val = get_sort_value(a)
local b_val = get_sort_value(b)
if a_val ~= b_val then
if order == "desc" then
return a_val > b_val
else
return a_val < b_val
end
return natural_sort_compare(a_val, b_val, order)
end
end
return a[FIELD_NAME] < b[FIELD_NAME]
Expand Down

0 comments on commit 4176bc7

Please sign in to comment.