zed icon indicating copy to clipboard operation
zed copied to clipboard

Add better file sorting options

Open TomPlanche opened this issue 1 year ago • 10 comments

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_sorting settings field in default.json with a strategy field.
  • Created SortStrategy enum with Lexicographical and Alphabetical variants.
  • 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:

  1. Add files_before_folders setting:

    {
        "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.

  2. Add uppercase_before_lowercase setting:

    {
        "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).

  3. 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
    }
}
  1. Last update, last created, ...

TomPlanche avatar Feb 19 '25 10:02 TomPlanche

@TomPlanche : Will this fix #24962 and #4533?

Angelk90 avatar Feb 19 '25 13:02 Angelk90

@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.

TomPlanche avatar Feb 19 '25 13:02 TomPlanche

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
image image

Angelk90 avatar Feb 19 '25 14:02 Angelk90

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.

TomPlanche avatar Feb 19 '25 14:02 TomPlanche

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?

iamnbutler avatar Feb 19 '25 21:02 iamnbutler

Drafting the PR, big bug found.

Screen Recording 2025-02-20 at 01 24 53

Trying to fix it. The tests passed, need to upgrade tests...

TomPlanche avatar Feb 20 '25 00:02 TomPlanche

@maxdeviant @iamnbutler what is the next move? Make alphabetical sorting the only option or leave the setting?

TomPlanche avatar Mar 18 '25 12:03 TomPlanche

@maxdeviant @iamnbutler @mikayla-maki any news ?

TomPlanche avatar Apr 18 '25 14:04 TomPlanche

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.

uberjay avatar May 13 '25 02:05 uberjay

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?

uberjay avatar May 13 '25 12:05 uberjay

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 avatar Jun 06 '25 20:06 probably-neb

@probably-neb

No problems !

Made the changes here.

Closing this PR.

TomPlanche avatar Jun 07 '25 19:06 TomPlanche