Skip to content

Instantly share code, notes, and snippets.

@zircote
Last active December 17, 2015 09:29
Show Gist options
  • Save zircote/5588042 to your computer and use it in GitHub Desktop.
Save zircote/5588042 to your computer and use it in GitHub Desktop.
<?php
usort($unsortedObjectArray, function( $a, $b ) {
if ($a->weight == $b->weight) {
return 0;
}
return ($a->weight < $b->weight) ? -1 : 1;
});
<?php
$words = array('abcd', 'hello', 'dogs', 'hiya');
$newWords = array();
foreach ($words as $word) {
$len = strlen($word);
if ($len > 4) {
$len = 4;
}
$i = 0;
$j = $len - 1;
$score = 0;
while ($i < $len) {
$byte = ord($word[$i]);
$score += $byte * pow(256, $j);
$i++;
$j--;
}
$newWords[$score] = $word;
}
ksort($newWords);
print_r($newWords);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment