doujiao8649 2016-04-03 14:30
浏览 51
已采纳

PHP按键中值的出现次数排序多维数组

I'm trying to first sort by quantity in Rank and if the quantity is equal then sort numerically by Rank.

It might sound complicated but here is the pseudo-code I envision

  1. First sort arrays by the number of repeated values in Rank.
  2. If number of repeated values is equal sort numerically by the value in Rank.

I guess its kinda recursive seeing as the 2nd part is performed on all sub-arrays.

I've been trying usort but I can't get it so see the count of repeated rank values in the array. Multisort doesn't seem to fit either.

Example:

Array
(
    [0] => Array
        (
            [Rank] => 7
            [Suit] => Hearts
        )

    [1] => Array
        (
            [Rank] => 3
            [Suit] => Hearts
        )

    [2] => Array
        (
            [Rank] => 6
            [Suit] => Spades
        )

    [3] => Array
        (
            [Rank] => 10
            [Suit] => Spades
        )

    [4] => Array
        (
            [Rank] => 3
            [Suit] => Spades
        )

    [5] => Array
        (
            [Rank] => 6
            [Suit] => Hearts
        )

    [6] => Array
        (
            [Rank] => 2
            [Suit] => Clubs
        )

)

According to my algorithm

Array
(

    [0] => Array
        (
            [Rank] => 6
            [Suit] => Hearts
        )

    [1] => Array
        (
            [Rank] => 6
            [Suit] => Spades
        )

    [2] => Array
        (
            [Rank] => 3
            [Suit] => Spades
        )


    [3] => Array
        (
            [Rank] => 3
            [Suit] => Hearts
        )

    [4] => Array
        (
            [Rank] => 10
            [Suit] => Spades
        )

    [5] => Array
        (
            [Rank] => 7
            [Suit] => Hearts
        )

    [6] => Array
        (
            [Rank] => 2
            [Suit] => Clubs
        )

)
  • 写回答

1条回答 默认 最新

  • douzhuiqing1151 2016-04-03 14:56
    关注

    The easiest solution I could come up with, is to use a second array in which we'll store the card count for each rank. We can then pass this array to the sorting function in order to get the result we want.

    Here's an example of how this would look:

    $cards = [...];
    
    $ranks = [];
    
    // Count the cards for each rank
    foreach ($cards as $card) {
        if (!isset($ranks[$card['Rank']])) {
            $ranks[$card['Rank']] = 0;
        }
    
        $ranks[$card['Rank']]++;
    }
    
    // Sort the cards array
    usort($cards, function ($a, $b) use ($ranks) {
        // If the cards count is the same for the rank, compare rank
        if ($ranks[$a['Rank']] == $ranks[$b['Rank']]) {
            return $a['Rank'] - $b['Rank'];
        }
    
        // Compare the card count for the rank
        return $ranks[$a['Rank']] - $ranks[$b['Rank']];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?