Skip to content

Instantly share code, notes, and snippets.

@JBlond
Created November 18, 2021 10:47
Show Gist options
  • Save JBlond/657a82fb8aa3d9f9aa1eb136fd267d6e to your computer and use it in GitHub Desktop.
Save JBlond/657a82fb8aa3d9f9aa1eb136fd267d6e to your computer and use it in GitHub Desktop.
tupleSort
<?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