Add better file sorting options
Release Notes:
- Improved file sorting.
As described in #20126, I was fed up with lexicographical file sorting in the project panel. The current sorting behavior doesn't handle numeric segments properly, leading to unintuitive ordering like
file_1.rs,file_10.rs,file_2.rs.
Overview
This PR adds a new setting's field file_sorting to control how files are sorted in the app.
For now, it introduces two sorting strategies:
-
lexicographical(default) - The existing sorting behavior -
alphabetical- Natural sorting that handles numeric segments properly
Implementation Details
- Added a new
file_sortingsettings field indefault.jsonwith astrategyfield. - Created
SortStrategyenum withLexicographicalandAlphabeticalvariants. - Implemented natural sorting algorithm that properly handles numeric segments in filenames.
- Updated path comparison functions to use the configured sort strategy.
- Modified relevant code in project panel and file finder to respect the sort strategy setting.
- Added unit tests to verify sorting behavior.
Example Configuration
{
"file_sorting": {
"strategy": "alphabetical" // or "lexicographical"
}
}
Example Sorting Results
Using lexicographical (default):
├── file_01.rs
├── file_1.rs
├── file_10.rs
├── file_1025.rs
├── file_2.rs
Using alphabetical (natural) sorting:
.
├── file_1.rs
├── file_01.rs
├── file_2.rs
├── file_10.rs
├── file_1025.rs
Improvements
Future improvements to the file sorting functionality could include:
-
Add
files_before_folderssetting:{ "file_sorting": { "files_before_folders": true // Controls whether files are listed before folders } }This would allow users to group all files before folders in the project panel, making it easier to find files when there are many nested directories.
-
Add
uppercase_before_lowercasesetting:{ "file_sorting": { "uppercase_before_lowercase": true // Controls whether uppercase filenames are sorted before lowercase } }This would give users control over case-sensitive sorting, which is particularly useful for projects that use capitalization conventions for certain types of files (e.g., README.md, LICENSE).
-
Add specific instructions for specific targets: If for the
project_panel, I want natural sorting but for the rest another stategy:
"file_sorting": {
// all options here will be default ones for everything
"strategy": "alphabetical",
"project_panel": {
"strategy": "lexicographical", // Overwritten fields
}
}
- Last update, last created, ...
@TomPlanche : Will this fix #24962 and #4533?
@Angelk90 It could easily for #24962, for #4533, the type or last modified could be the next step as explained in ## Improvements of the PR comment.
I'm not sure about the #4533 Say right-click a folder and change the sort order of the files but listing folders first.
Example here we have two different sorting types, both are from Microsoft.
GitHub uses case-sensitive sorting (where uppercase letters come before lowercase ones) while VS Code uses case-insensitive sorting (ignoring whether letters are uppercase or lowercase when comparing them)
| Github | Vscode |
|---|---|
Yes, as said in ## Improvements, we can add fields in the file_sorting object for these.
I'll maybe add those later, now we have the main brick for sorting settings.
Is there any benefit to the lexicographical sorting style we have now? If not, should we just move to alphabetical by default?
Basically, should this be a setting or should we just change the way it works be default?
Drafting the PR, big bug found.
Trying to fix it. The tests passed, need to upgrade tests...
@maxdeviant @iamnbutler what is the next move? Make alphabetical sorting the only option or leave the setting?
@maxdeviant @iamnbutler @mikayla-maki any news ?
I'm very excited about the prospect of alphabetical sorting... Having the macOS order firmly baked into my brain, I find the Windows order to be jarring and confusing multiple times per day. 😭
It also has the unfortunate side-effect of distancing module.rs from module/submodule.rs.
One additional note: this change proposes the current sorting be named "lexicographical" -- I'm used to this meaning (roughly) alphabetical. Do you have a reference to anyone using "lexicographical" to describe Windows-style sorting?
Hey, sorry this was left behind for so long. I vote we make your method the default as @iamnbutler suggested. Happy to help or take over if you'd like. Sorry again for the delay
@probably-neb
No problems !
Made the changes here.
Closing this PR.