Skip to content

Fix natural case insensitive sort for files #1215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/guides/src/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use function in_array;
use function sort;

use const SORT_FLAG_CASE;
use const SORT_NATURAL;

/** @implements IteratorAggregate<string> */
final class Files implements IteratorAggregate, Countable
{
Expand All @@ -35,7 +38,7 @@ public function add(string $filename): void
}

$this->files[] = $filename;
sort($this->files);
sort($this->files, SORT_NATURAL | SORT_FLAG_CASE);
}

/** @return Iterator<string> */
Expand Down
40 changes: 40 additions & 0 deletions packages/guides/tests/unit/FilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides;

use PHPUnit\Framework\TestCase;

use function iterator_to_array;

class FilesTest extends TestCase
{
public function testFilesAreSorted(): void
{
$files = new Files();
$files->add('page');
$files->add('Subpage');
$files->add('index');

$result = iterator_to_array($files->getIterator());

self::assertSame(
[
'index',
'page',
'Subpage',
],
$result,
);
}
}
Loading