Created
November 18, 2021 10:47
-
-
Save JBlond/657a82fb8aa3d9f9aa1eb136fd267d6e to your computer and use it in GitHub Desktop.
tupleSort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sort an array by the nested arrays it contains. Helper function for getMatchingBlocks | |
* | |
* @param array $aArray First array to compare. | |
* @param array $bArray Second array to compare. | |
* | |
* @return int -1, 0 or 1, as expected by the usort function. | |
*/ | |
public static function tupleSort(array $aArray, array $bArray): int | |
{ | |
$max = max(count($aArray), count($bArray)); | |
for ($counter = 0; $counter < $max; ++$counter) { | |
if ($aArray[$counter] < $bArray[$counter]) { | |
echo('A'); | |
return -1; | |
} | |
if ($aArray[$counter] > $bArray[$counter]) { | |
echo('B'); | |
return 1; | |
} | |
} | |
if (count($aArray) == count($bArray)) { | |
echo('C'); | |
return 0; | |
} | |
if (count($aArray) < count($bArray)) { | |
echo('D'); | |
return -1; | |
} | |
echo('E'); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment