Oihana PHP

sortFiles.php

Table of Contents

Functions

sortFiles()  : void
Sorts an array of SplFileInfo objects.

Functions

sortFiles()

Sorts an array of SplFileInfo objects.

sortFiles(array<string|int, SplFileInfo&$files, callable|string|array<string|int, mixed> $sort[, string|null $order = 'asc' ]) : void
Parameters
$files : array<string|int, SplFileInfo>

Array of files to sort (modified in‑place).

$sort : callable|string|array<string|int, mixed>

One of:

  • callable : custom compare function, ex: fn(SplFileInfo $a, SplFileInfo $b): int
  • string : single built‑in key
    'name' | 'ci_name' | 'extension' | 'size' | 'type' | 'atime' | 'ctime' | 'mtime'
  • array : ordered list of such keys for multi‑criteria sorting e.g. ['type', 'name'] or ['extension','size']
$order : string|null = 'asc'

The direction of the sort method 'asc' (default) or 'desc'.

Tags
examples
// 1) Sort by filename ascending
sortFiles($files, 'name');

// 2) Case‑insensitive filename descending
sortFiles($files, 'ci_name', 'desc');

// 3) Sort by extension then by size
sortFiles($files, ['extension', 'size']);

// 4) Custom comparator: modified time descending
sortFiles($files, fn($a, $b) => $a->getMTime() <=> $b->getMTime(), 'desc');

// 5) Type then case‑insensitive name
sortFiles($files, ['type', 'ci_name']);
author

Marc Alcaraz (ekameleon)

since
1.0.0

        
On this page

Search results