首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按子数组对多维数组排序

按子数组对多维数组排序
EN

Stack Overflow用户
提问于 2013-02-03 23:19:44
回答 2查看 103关注 0票数 0

我有下面的数组,我尝试按分数排序,然后是匹配,然后是名称,但我的方法不起作用。有谁知道为什么吗?

最后的顺序应该是4,3,5。

我使用的usort在底部。

代码语言:javascript
复制
        [3] => Array
            (
                [name] => DrayTek Vigor 2130Vn VoIP/WiFi Router
                [matches] => Array
                    (
                        [0] => voip
                    )
                [score] => 3
            )
        [4] => Array
            (
                [name] => DrayTek Vigor 2750n VDSL Wireless Router
                [matches] => Array
                    (
                        [0] => 2750
                    )
                [score] => 3
            )
        [5] => Array
            (
                [name] => DrayTek Vigor 2850Vn VDSL/ADSL VoIP Router
                [matches] => Array
                    (
                        [0] => voip
                    )
                [score] => 3
            )

逻辑

代码语言:javascript
复制
1. all have the same score, so no change in order 
2. 4 has 2750 in matches[0] which assuming numbers come before letters, moves 4 up
** the order now should be 4,3,5
3. as 3 and 5 have the same matches[], no change in order
4. 3's name naturally comes before 5 but since its already above, no change
** final order should be 4,3,5

对结果进行排序,首先是最高分,然后是匹配数组,然后是名称

代码语言:javascript
复制
function cmp($a, $b)
{
    if ( $a['score'] < $b['score'] )
        return 1;
    elseif ( $a['score'] > $b['score'] )
        return -1;
    elseif ( ! array_diff( $a['matches'], $b['matches'] ) )
        return 1;
    elseif ( ! array_diff( $b['matches'], $a['matches'] ) )
        return -1;
    elseif ( ($c = strnatcmp( strtolower($a['name']), strtolower($b['name']) ) ) !== 0 )
        return $c;
    else
        return 0;
}
usort( $this->results['rows'], "cmp" );
EN

回答 2

Stack Overflow用户

发布于 2013-02-04 00:21:02

您似乎颠倒了matches数组比较的含义(如果它们相等,则返回1,而不是返回0/让它传递到下一个测试)。因为当它们不相等时,你需要一个明确的顺序,也许你应该按matches数组的长度排序:

代码语言:javascript
复制
function cmp($a, $b)
{
    # sort by score 
    $result = $b['score'] - $a['score'];

    # then by number of matches
    if ($result == 0) {
      $result = count($b['matches']) - count($a['matches']);
    }

    # if they have the same number of matches but different matches, who wins?
    if ($result == 0) {
      $result = strnatcasecmp($a['name'], $b['name']);
    }

    return $result;
}

array_diff的问题是它返回单个数组。为了得到a和b排序,您将结果与什么进行比较?比较函数需要能够在没有任何其他上下文的情况下从数组的其余部分对任意两个项进行排序。

票数 0
EN

Stack Overflow用户

发布于 2013-02-04 08:48:58

找到了解决方案

代码语言:javascript
复制
function cmp($a, $b)
{
    if ( $a['score'] < $b['score'] )
        return 1;

    if ( $a['score'] > $b['score'] )
        return -1;

    if ( count( $a['matches'] ) > count( $b['matches'] ) )
        return 1;

    if ( count( $a['matches'] ) < count( $b['matches'] ) )
        return -1;

    natsort( $a['matches'] );   natsort( $b['matches'] );

    for ( $i = 0; $i < count( $a['matches'] ); $i++ )
    {
        if ( ( $c = strnatcasecmp( $b['matches'][$i], $a['matches'][$i] ) ) !== 0)
            return $c;
    }

    if ( ( $c = strnatcasecmp( strtolower($a['name'] ), strtolower( $b['name'] ) ) ) !== 0 )
        return $c;

    return 0;
}

usort( $this->results['rows'], "cmp" );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14673969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档