3v4l.org

run code in 300+ PHP versions simultaneously
<?php $indexes = [ 26 => ["namespace" => "X\\Y\\Zed"], 9 => ["namespace" => "A\\B\\See"], 3 => ["namespace" => "A\\B\\Bee"], 38 => ["namespace" => "A\\B\\C\\Dee"], 51 => ["namespace" => "X\\Wye"], 16 => ["namespace" => "A\\Sea"], 12 => ["namespace" => "A\\Bees"], ]; uasort( $indexes, static function (array $a, array $b) { $aPath = $a['namespace']; $bPath = $b['namespace']; // Just in case there are duplicates if ($aPath === $bPath) { return 0; } // Break into parts $aParts = explode('\\', $aPath); $bParts = explode('\\', $bPath); // If we only have a single thing then it is a root-level, just compare the item if (1 === count($aParts) && 1 === count($bParts)) { return $aPath <=> $bPath; } // Get the class and namespace (file and folder) parts $aClass = array_pop($aParts); $bClass = array_pop($bParts); $aNamespace = implode('\\', $aParts); $bNamespace = implode('\\', $bParts); // If the namespaces are the same, sort by class name if ($aNamespace === $bNamespace) { return $aClass <=> $bClass; } // If the first namespace _starts_ with the second namespace, sort it first if (0 === mb_strpos($aNamespace, $bNamespace)) { return -1; } // Same as above but the other way if (0 === mb_strpos($bNamespace, $aNamespace)) { return 1; } // Just only by namespace return $aNamespace <=> $bNamespace; } ); $expected = [ 38 => ["namespace" => "A\\B\\C\\Dee"], 3 => ["namespace" => "A\\B\\Bee"], 9 => ["namespace" => "A\\B\\See"], 12 => ["namespace" => "A\\Bees"], 16 => ["namespace" => "A\\Sea"], 26 => ["namespace" => "X\\Y\\Zed"], 51 => ["namespace" => "X\\Wye"], ]; assert($indexes === $expected);

preferences:
29.03 ms | 408 KiB | 5 Q