带uasort的排序函数


Sorting Function with uasort

我试图为我的多维数组创建一个排序函数,但我无法找到算法,。

下面是我想对排序的数组的一个例子

[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
    )

我想按照soldAvg和inStock 之间最大差异的顺序对数组进行排序

所以数组应该像下面的

 [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
    )

我真正头疼的是,当inStock大于的平均销售额时,我不知道该怎么办

我唯一能做的就是

 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;
 }

但是,如果inStock大于soldAvg,它就不起作用,如果soldAvg为0,我会得到这个错误"除以零"

我知道你已经有一段时间没有发布这个问题了,但我最近做了一些类似于你所问的内容,因为它可能会帮助stackoverflow的其他访问者,所以现在就开始:

/**
 * 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;
}

示例:

$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);

超出预期:

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
        )
)