对数组进行排序:首先按定义的顺序按 1 个元素排序,然后按其他 2 个元素的总数排序

Sorting an array: Firstly by 1 element in a defined order, Then by the total of 2 other elements

我看过很多关于 SO 的例子,但我找不到解决这个问题的方法。我有一个大数组,我需要按 2 个元素对其进行排序。

  • Firstly, by a priority field in a custom order: S1,S2,S3,S4,S5,D,NS

  • Secondly, by 2 fields in the array added together. (This bit is working.)

我已经制作了一个排序函数,它将按上述两种方式排序,但是,它在 S1-S5 之前对 D 和 NS 进行排序,因为 D 按字母顺序排在第一位。 - 因此我需要定义一个数组,其中包含我想要排序的自定义顺序。请问有人能帮忙吗?

我使用的函数是:

function compdaily($a, $b) {
    if ($a['Priority'] == $b['Priority']) 
    {
        return ($a['Value1'] + $a['Value2']) < ($b['Value1'] + $b['Value2']);
    }
    return strcmp($a['Priority'], $b['Priority']);
}

然后我以这种方式调用上面的函数:

usort($Array, 'compdaily');

按以下顺序列出项目:

Array
(
    [0] => Array
        (
            [SomeID] => 1234
            [Priority] => D
            [Value1] => 250
            [Value2] => 100
        )
    [1] => Array
        (
            [SomeID] => 456
            [Priority] => NS
            [Value1] => 250
            [Value2] => 100
        )
    [2] => Array
        (
            [SomeID] => 124
            [Priority] => S1
            [Value1] => 200
            [Value2] => 200
        )
    [3] => Array
        (
            [SomeID] => 1235
            [Priority] => S1
            [Value1] => 100
            [Value2] => 100
        )
    [4] => Array
        (
            [SomeID] => 1230
            [Priority] => S2
            [Value1] => 250
            [Value2] => 100
        )
    [5] => Array
        (
            [SomeID] => 123495
            [Priority] => S3
            [Value1] => 250
            [Value2] => 100
        )
    [6] => Array
        (
            [SomeID] => 123498
            [Priority] => S3
            [Value1] => 100
            [Value2] => 100
        )
    [7] => Array
        (
            [SomeID] => 12345
            [Priority] => S4
            [Value1] => 250
            [Value2] => 100
        )
)

但是,我需要它们的顺序是:

Array
(
    [0] => Array
        (
            [SomeID] => 124
            [Priority] => S1
            [Value1] => 200
            [Value2] => 200
        )
    [1] => Array
        (
            [SomeID] => 1235
            [Priority] => S1
            [Value1] => 100
            [Value2] => 100
        )
    [2] => Array
        (
            [SomeID] => 1230
            [Priority] => S2
            [Value1] => 250
            [Value2] => 100
        )
    [3] => Array
        (
            [SomeID] => 123495
            [Priority] => S3
            [Value1] => 250
            [Value2] => 100
        )
    [4] => Array
        (
            [SomeID] => 123498
            [Priority] => S3
            [Value1] => 100
            [Value2] => 100
        )
    [5] => Array
        (
            [SomeID] => 12345
            [Priority] => S4
            [Value1] => 250
            [Value2] => 100
        )
    [6] => Array
        (
            [SomeID] => 1234
            [Priority] => D
            [Value1] => 250
            [Value2] => 100
        )
    [7] => Array
        (
            [SomeID] => 456
            [Priority] => NS
            [Value1] => 250
            [Value2] => 100
        )
)

我已经尝试使用示例:Sort an array by multiple keys in a certain order 但是我无法让它与上面的一起工作。

与其直接在排序中使用 $a['Priority'],不如使用查找 table 来获取实际顺序

类似...

$mapp = array('S1'=>1, 'S2'=>2, 'S3'=>3, 'S4'=>4, 'S5'=>5, 'D'=>6, 'NS'=>7);

function compdaily($a, $b) {
    global $mapp;
    ...
    return ($mapp[$a['Priority']] < $mapp[$b['Priority']]);
}

(if语句不用改,直接比较字符串是否相等)