Skip to content

Fix image navigation order: natural sort instead of lexicographic#158

Open
Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Slashxdd:fix/natural-sort-file-navigation
Open

Fix image navigation order: natural sort instead of lexicographic#158
Slashxdd wants to merge 1 commit intoModuleArt:feature/new-webp-decoderfrom
Slashxdd:fix/natural-sort-file-navigation

Conversation

@Slashxdd
Copy link
Copy Markdown

Fixes #146

Problem

When navigating through images in a folder with arrow buttons, files are ordered lexicographically: 1, 101, 2 — instead of the expected natural order 1, 2, 101.

Root cause

GetCurrentFiles() builds the file list from Directory.GetFiles() and returns it as-is, with no sorting applied. Directory.GetFiles() returns files in filesystem/lexicographic order, so multi-digit filenames end up out of order.

Fix

Sort the list in GetCurrentFiles() using StrCmpLogicalW from shlwapi.dll — the exact same Windows shell function that File Explorer uses for its own file sorting. This guarantees the navigation order always matches what the user sees in Explorer.

[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string x, string y);

// in GetCurrentFiles():
arlist.Sort((a, b) => StrCmpLogicalW(Path.GetFileName(a), Path.GetFileName(b)));

One line added to GetCurrentFiles(), one DllImport declaration, one using System.Runtime.InteropServices.

Resolves ModuleArt#146: images now navigate in natural order (1, 2, 101)
matching Windows Explorer, instead of lexicographic order (1, 101, 2).

Uses StrCmpLogicalW from shlwapi.dll — the same Windows shell function
Explorer itself uses for file sorting.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant