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 | local function fuzzy_find(needle, haystacks, case_sensitive) | 411 | local function fuzzy_find(needle, haystacks, case_sensitive) |
| 412 | local result = require 'mp.fzy'.filter(needle, haystacks, case_sensitive) | 412 | local result = require 'mp.fzy'.filter(needle, haystacks, case_sensitive) |
| 413 | if line ~= '' then -- Prevent table.sort() from reordering the items. | 413 | table.sort(result, function (i, j) |
| 414 | table.sort(result, function (i, j) | 414 | if i[3] ~= j[3] then |
| 415 | return i[3] > j[3] | 415 | return i[3] > j[3] |
| 416 | end) | 416 | end |
| 417 | end | 417 | |
| 418 | return i[1] < j[1] | ||
| 419 | end) | ||
| 418 | for i, value in ipairs(result) do | 420 | for i, value in ipairs(result) do |
| 419 | result[i] = value[1] | 421 | result[i] = value[1] |
| 420 | end | 422 | end |