From 0d38d1bbc5ac113bc8fd29a4ad71f0fc2aed8010 Mon Sep 17 00:00:00 2001 From: Anton Dyshkant Date: Tue, 24 Apr 2018 15:03:14 +0300 Subject: [PATCH 1/2] [Finder] Described "use natural sort" option --- components/finder.rst | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/components/finder.rst b/components/finder.rst index 790e0cf7bdc..c115e0005f6 100644 --- a/components/finder.rst +++ b/components/finder.rst @@ -168,10 +168,10 @@ Sort the result by name or by type (directories first, then files):: $finder->sortByType(); -.. note:: +.. tip:: - Notice that the ``sort*`` methods need to get all matching elements to do - their jobs. For large iterators, it is slow. + To sort by name using `natural sort order`_ algorithm, + pass ``true`` as the first argument of ``sortByName()`` method. You can also define your own sorting algorithm with ``sort()`` method:: @@ -179,6 +179,11 @@ You can also define your own sorting algorithm with ``sort()`` method:: return strcmp($a->getRealPath(), $b->getRealPath()); }); +.. note:: + + Notice that the ``sort*`` methods need to get all matching elements to do + their jobs. For large iterators, it is slow. + File Name ~~~~~~~~~ @@ -320,8 +325,9 @@ The contents of returned files can be read with // ... } -.. _strtotime: https://php.net/manual/en/datetime.formats.php -.. _protocol: https://php.net/manual/en/wrappers.php -.. _Streams: https://php.net/streams -.. _IEC standard: https://physics.nist.gov/cuu/Units/binary.html -.. _Packagist: https://packagist.org/packages/symfony/finder +.. _strtotime: https://php.net/manual/en/datetime.formats.php +.. _protocol: https://php.net/manual/en/wrappers.php +.. _Streams: https://php.net/streams +.. _IEC standard: https://physics.nist.gov/cuu/Units/binary.html +.. _Packagist: https://packagist.org/packages/symfony/finder +.. _`natural sort order`: https://en.wikipedia.org/wiki/Natural_sort_order From 96498001740f7757cdff9788970b47f97fd6ec42 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 1 Jun 2018 10:34:31 +0200 Subject: [PATCH 2/2] Expanded the explanation a bit --- components/finder.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/finder.rst b/components/finder.rst index c115e0005f6..22b2ae41522 100644 --- a/components/finder.rst +++ b/components/finder.rst @@ -170,8 +170,13 @@ Sort the result by name or by type (directories first, then files):: .. tip:: - To sort by name using `natural sort order`_ algorithm, - pass ``true`` as the first argument of ``sortByName()`` method. + By default, the ``sortByName()`` method uses the :phpfunction:`strcmp` PHP + function (e.g. ``file1.txt``, ``file10.txt``, ``file2.txt``). Pass ``true`` + as its argument to use PHP's `natural sort order`_ algorithm instead (e.g. + ``file1.txt``, ``file2.txt``, ``file10.txt``). + + .. versionadded:: 4.2 + The option to use the natural sort order was introduced in Symfony 4.2. You can also define your own sorting algorithm with ``sort()`` method::