public static sortFilesDescByPathLength ( string[] $files ) : string[] | ||
$files | string[] | |
return | string[] |
public static function sortFilesDescByPathLength($files)
{
usort($files, function ($a, $b) {
// sort by filename length so we kinda make sure to remove files before its directories
if ($a == $b) {
return 0;
}
return strlen($a) > strlen($b) ? -1 : 1;
});
return $files;
}
public function test_sortFilesDescByPathLength_shouldOrderDesc_IfDifferentLengthsGiven() { $input = array('xyz/1.gif', '1.gif', 'x', 'x/xyz.gif', 'xyz', 'xxyyzzgg', 'xyz/long.gif'); $result = Filesystem::sortFilesDescByPathLength($input); $expected = array('xyz/long.gif', 'x/xyz.gif', 'xyz/1.gif', 'xxyyzzgg', '1.gif', 'xyz', 'x'); $this->assertEquals($expected, $result); }