Skip to content

[RFC] Improve the "sortByName" algorithm of the Finder — apply "natural ordering" (SortableIterator::SORT_BY_NAME) #26930

Closed
@vyshkant

Description

@vyshkant
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 3.4.6

Using an strcmp algorithm for sorting by name leads to unexpected results.

Suppose we have the next code:

$finder = new Finder();

$finder->files()->sortByName()->in('/path/to/directory');

foreach ($finder as $file) {
    echo $file->getBasename().PHP_EOL;
}

We are trying to order the next set of files:

Files as they are ordered in file manager
foo_0.txt
foo_1.txt
foo_2.txt
foo_10.txt
foo_11.txt
foo_12.txt
foo_100.txt
foo_101.txt
foo_102.txt
foo_1000.txt
foo_1001.txt
foo_1002.txt

The order shown above seems to be "expected", because it is used in many of popular file managers ("File Explorer", "Windows Explorer" on Windows, "Nautilus", "Caja" on Linux, etc).

Nevertheless, when you try to get ordered list of these files using Finder, you will be surprized:

Files as they are ordered by Finder
foo_0.txt
foo_1.txt
foo_10.txt
foo_100.txt
foo_1000.txt
foo_1001.txt
foo_1002.txt
foo_101.txt
foo_102.txt
foo_11.txt
foo_12.txt
foo_2.txt

Moreover, sorting the next file set (as it is shown in a file manager):

Files as they are ordered in file manager (1)
foo_1_1.txt
foo_2_1.txt
foo_10_1.txt
foo_11_1.txt
foo_12_1.txt
foo_100_1.txt
foo_101_1.txt
foo_102_1.txt
foo_1000_1.txt
foo_1001_1.txt
foo_1002_1.txt

will give the next result:

Files as they are ordered by Finder (1)
foo_1000_1.txt
foo_1001_1.txt
foo_1002_1.txt
foo_100_1.txt
foo_101_1.txt
foo_102_1.txt
foo_10_1.txt
foo_11_1.txt
foo_12_1.txt
foo_1_1.txt
foo_2_1.txt

I think this kind of sorting is wrong.

What do I suggest

My suggestion is to create a sorting algorythm, which will return "the same as file manager" files order.

I believe that ideally this new algorithm should be used when calling the "sortByName" method, but even if not, this should be implemented at the Finder component level, because this is a quite common task.

If you agree with my arguments, I will gladly write a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureFinderRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions