table.sort() is unstable, meaning that it randomly reorders items with the same score, so currently fuzzy_find() checks if the line is empty to not change the original order of the unsorted items. But actually filled lines have the same problem: e.g. searching foo with filenames like foo_{1..99}, they are returned in random order. To fix this sort by score only when 2 items have different score, else sort by the original position.
Unified · Split
| @@ -410,11 +410,13 @@ end · 409 unmodified lines | ||
| 410 | 410 | |
| 411 | 411 | local function fuzzy_find(needle, haystacks, case_sensitive) |
| 412 | 412 | local result = require 'mp.fzy'.filter(needle, haystacks, case_sensitive) |
| 413 | − if line ~= '' then -- Prevent table.sort() from reordering the items. | |
| 414 | − table.sort(result, function (i, j) | |
| 413 | + table.sort(result, function (i, j) | |
| 414 | + if i[3] ~= j[3] then | |
| 415 | 415 | return i[3] > j[3] |
| 416 | − end) | |
| 417 | − end | |
| 416 | + end | |
| 417 | + | |
| 418 | + return i[1] < j[1] | |
| 419 | + end) | |
| 418 | 420 | for i, value in ipairs(result) do |
| 419 | 421 | result[i] = value[1] |
| 420 | 422 | end |