Im trying to create a sorting function for my multidimensional array but I can't figure out the algorithm,.
The following is an example of the array I want to sort
[test1] => Array
(
[soldAvg] => 3
[inStock] => 100
)
[test2] => Array
(
[soldAvg] => 3
[inStock] => 0
)
[test3] => Array
(
[soldAvg] => 113
[inStock] => 31
)
[test4] => Array
(
[soldAvg] => 4
[inStock] => 1
)
[test5] => Array
(
[soldAvg] => 3
[inStock] => 1
)
I want to sort the array in order of the biggest diffence between soldAvg and inStock
So array should be like the following
[test1] => Array
(
[soldAvg] => 3
[inStock] => 100
)
[test3] => Array
(
[soldAvg] => 113
[inStock] => 31
)
[test4] => Array
(
[soldAvg] => 4
[inStock] => 1
)
[test2] => Array
(
[soldAvg] => 3
[inStock] => 0
)
[test5] => Array
(
[soldAvg] => 3
[inStock] => 1
)
My real headache is that I dont know what to do when inStock is bigger than sold avg
The only thing that I have near working is
function usortAlgo($a,$b)
{
if($a['soldAvg']*2 / $a['inStock'] == $b['soldAvg']*2 / $b['inStock'])
return 0;
if($a['soldAvg']*2 / $a['inStock'] > $b['soldAvg']*2 / $b['inStock'])
return -1;
if($a['soldAvg']*2 / $a['inStock'] < $b['soldAvg']*2 / $b['inStock'])
return 1;
}
But if inStock is bigger than soldAvg it doesnt work and if soldAvg is 0 I get this error "Division by zero"
I know it's been a while since you've posted this question but I've recently made something similar to what you're asking and since it might help other visitors of stackoverflow here you go:
/**
* function used in sortByDiff() sort the array
* @param mixed $a [description]
* @param mixed $b [description]
* @return int The comparison function must return an integer less than, equal to,
* or greater than zero if the first argument is considered to be
* respectively less than, equal to, or greater than the second.
*/
function cmp($a, $b) {
if ($a == $b)
return 0;
return ($a['diff'] < $b['diff']) ? -1 : 1;
}
/**
* sort an array in order of the diffence between to nested values
* @param array $toSort the array to sort
* @param String $valueName1 name of the first index
* @param String $valueName2 name of the second index
* @param boolean $rev true|false reverse the order if true
* @return array the sorted array
*/
function sortByDiff(array $toSort, $valueName1, $valueName2, $rev = false)
{
$sorted = $toSort;
foreach ($toSort as $key => $elem) {
$val1 = getValue($elem, array($valueName1));
$val2 = getValue($elem, array($valueName2));
if ($val1 === null || $val2 === null)
return null;
$sorted[$key]['diff'] = abs($elem[$valueName1] - $elem[$valueName2]);
}
uasort($sorted, 'cmp');
foreach ($sorted as $key => $elem)
unset($sorted[$key]['diff']);
if ($rev === true)
return array_reverse($sorted);
return $sorted;
}
/**
* @param array $array The array from which to get the value
* @param array $parents An array of parent keys of the value,
* starting with the outermost key
* @param bool $key_exists If given, an already defined variable
* that is altered by reference
* @return mixed The requested nested value. Possibly NULL if the value
* is NULL or not all nested parent keys exist.
* $key_exists is altered by reference and is a Boolean
* that indicates whether all nested parent keys
* exist (TRUE) or not (FALSE).
* This allows to distinguish between the two
* possibilities when NULL is returned.
*/
function &getValue(array &$array, array $parents, &$key_exists = NULL)
{
$ref = &$array;
foreach ($parents as $parent) {
if (is_array($ref) && array_key_exists($parent, $ref))
$ref = &$ref[$parent];
else {
$key_exists = FALSE;
$null = NULL;
return $null;
}
}
$key_exists = TRUE;
return $ref;
}
Example:
$toSort = [
'test1' => [
'soldAvg' => 3,
'intStock' => 100,
],
'test2' => [
'soldAvg' => 3,
'intStock' => 0,
],
'test3' => [
'soldAvg' => 113,
'intStock' => 31,
],
'test4' => [
'soldAvg' => 4,
'intStock' => 1,
],
'test5' => [
'soldAvg' => 3,
'intStock' => 1,
],
];
$sorted = sortByDiff($toSort, 'soldAvg', 'intStock', true);
print_r($sorted);
Outup as expected:
Array
(
[test1] => Array
(
[soldAvg] => 3
[intStock] => 100
)
[test3] => Array
(
[soldAvg] => 113
[intStock] => 31
)
[test2] => Array
(
[soldAvg] => 3
[intStock] => 0
)
[test4] => Array
(
[soldAvg] => 4
[intStock] => 1
)
[test5] => Array
(
[soldAvg] => 3
[intStock] => 1
)
)