Created
November 2, 2012 10:11
-
-
Save JonasDoebertin/3999936 to your computer and use it in GitHub Desktop.
Sort emails by domain and mailbox
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
/** | |
* Sort an array with emails by their domains and mailbox | |
* ------------------------------------------------------ | |
* Input: | |
* array( | |
* 'bbb@ccc.de', | |
* 'bbb@bbb.de', | |
* 'ccc@aaa.de', | |
* 'aaa@aaa.de', | |
* 'aaa@bbb.de' | |
* ); | |
* | |
* Output: | |
* array( | |
* 'aaa@aaa.de', | |
* 'ccc@aaa.de', | |
* 'aaa@bbb.de', | |
* 'bbb@bbb.de', | |
* 'bbb@ccc.de' | |
* ); | |
* ------------------------------------------------------ | |
* @param Array | |
* @return Array | |
*/ | |
function sort_emails(array &$emails){ | |
function sort_by_domain($a, $b){ | |
list($aMailbox, $aDomain) = explode('@', $a); | |
list($bMailbox, $bDomain) = explode('@', $b); | |
if( $aDomain == $bDomain ){ | |
return ($aMailbox < $bMailbox)? -1 : 1; | |
} | |
return ($aDomain < $bDomain)? -1 : 1; | |
} | |
usort($emails, 'sort_by_domain'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment