Created
February 11, 2015 03:06
-
-
Save iamkirkbater/9e85e818b22993852169 to your computer and use it in GitHub Desktop.
Sort by title algorithms
This file contains 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
/** | |
* @param $a | |
* @param $b | |
* @return int | |
* | |
* Assumes that the list is already sorted alphabetically, this function will sort Directors, Associate Directors, and | |
* Assistant directors to the front of the list, respectively. | |
*/ | |
function sortByTitle ($a, $b) | |
{ | |
$aTitle = $a->Title; | |
$bTitle = $b->Title; | |
$titleOrder = array( | |
'Director', | |
'Associate Director', | |
'Assistant Director', | |
); | |
foreach($titleOrder as $title) | |
{ | |
if($aTitle == $title) | |
{ | |
return -1; | |
} | |
else if($bTitle == $title) | |
{ | |
return 1; | |
} | |
} | |
return 0; | |
} |
This file contains 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
function sortByTitle ($a, $b) | |
{ | |
static $sorted = array( | |
'Director' => 3, | |
'Associate Director' => 2, | |
'Assistant Director' => 1, | |
); | |
if ($sorted[$a->Title] > $sorted[$b->Title]) { | |
return -1; | |
} elseif ($sorted[$a->Title] < $sorted[$b->Title]) { | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment