Last active
June 17, 2019 01:01
-
-
Save matthewstokeley/a08792e66f2cfa13d36de371d322527b to your computer and use it in GitHub Desktop.
php sorting methods
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
<?php | |
$arr = [ | |
[ | |
"key" => "value", | |
"id" => 1 | |
], [ | |
"key" => "value", | |
"id" => 0 | |
] | |
]; | |
class SortFunctionReceivesCorrectValueTest | |
{ | |
/** | |
* [$arr description] | |
* @var array | |
*/ | |
protected $arr = [ | |
[ | |
"key" => "value", | |
"id" => 1 | |
], [ | |
"key" => "value", | |
"id" => 0 | |
] | |
]; | |
protected function setUp():void { | |
} | |
protected function sort(&$arr, Array $value, Array $sortableKey): bool { | |
$res = false; | |
foreach ($value as $k => $v) { | |
if ($k == $sortableKey) { | |
$res = true; | |
} | |
} | |
return $res; | |
} | |
/** | |
* Test the sort function to ensure that the | |
* user defined key is in the test array | |
* @throws \Exception | |
* @return void | |
*/ | |
public function testSortByKeySendingCorrectKey(): void { | |
foreach ($this->arr as $key => $value) { | |
if ($res = $this->sort($this->arr, $value) === false) { | |
throw new \Exception('sort by key does not send a required key'); | |
} | |
} | |
} | |
} | |
try { | |
$test = new SortFunctionReceivesCorrectValueTest(); | |
$test->testSortByKeySendingCorrectKey(); | |
} catch (\Exception $e) { | |
throw new \Exception($e->getMessage()); | |
} | |
class IsArraySortedByKeyValueTest | |
{ | |
/** | |
* The array that we are testing | |
* @var Array | |
*/ | |
protected $array; | |
/** | |
* A setup function | |
* @return Void | |
*/ | |
protected function setup():void { | |
} | |
public function testIfArrayIsSortedByKeyValue() { | |
} | |
} | |
class SortingUtilities | |
{ | |
/** | |
* @todo refactor to be functional | |
* | |
*/ | |
public static swap(Array$arr, String $key, String $value): Void | |
{ | |
$current = $value; | |
$next = $arr[$k + 1]; | |
$arr[$k + 1] = $current; | |
$arr[$k] = $next; | |
} | |
/** | |
* [bubbleSortByKey description] | |
* @param Array $arr | |
* @param String $sortableKey The key to sort for | |
* @return Array [description] | |
*/ | |
public static function bubbleSortByKey(Array $arr, String $sortableKey) | |
{ | |
$table = [ | |
//'true' => static::bubbleSortByKey(), | |
//false => '' | |
]; | |
function sort(&$arr, String $value): Array { | |
// if is_array() | |
foreach ($value as $k => $v) { | |
if ((!$k == $sortableKey) && (is_array($v))) { | |
sort($v); | |
} | |
if (($k == $sortableKey ) && ($v < $arr[$k + 1])) { | |
$current = $v; | |
$next = $arr[$k + 1]; | |
$arr[$k + 1] = $current; | |
$arr[$k] = $next; | |
// static::swap($arr, $k, $v); | |
// $recurse = true | |
sort($value); | |
} | |
// $table[$recurse](); | |
} | |
return $arr; | |
} | |
sort($arr); | |
return $arr; | |
} | |
/** | |
* Quicksort By Key | |
* @todo finish | |
* @param Array $arr | |
* @param String $sortableKey | |
* @return Array | |
*/ | |
public static function quickSortByKey(Array $arr, String $sortableKey): Array | |
{ | |
function quicksort($array, $low, $high) | |
{ | |
$partitionResult = partition($array, $low, $high); | |
quicksort($array, $low, $partitionResult - 1); | |
quicksort($array, $partitionResult + 1, $high); | |
} | |
function partition($array, $low, $high) | |
{ | |
$pivot = $array[$high]; | |
$ii = $low; | |
for ($i = $low; $i < $high; $i++) { | |
if ($i > $pivot) { | |
if ($i != $ii) { | |
} | |
} | |
} | |
} | |
} | |
} | |
print_r(SortingUtilities::bubbleSortByKey($arr, 'id')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment