douyi7055 2013-05-07 08:02
浏览 153
已采纳

对多维数组进行排序和随机排序

I am working on gaming project, which needs sort and shuffle multidimensional array. First i need to sort based on bid. If multiple bids are same i need to sort based on priority. If bid & priority both are same means I need to shuffle those elements. For example we have 3 array elements in bid=0.4 & priority=4. Id's are 102,103 & 104. These array element position should be shuffled.

array(
    array('id' => 101, 'bid' => 0.5, 'priority' => 5),
    array('id' => 102, 'bid' => 0.4, 'priority' => 4),
    array('id' => 103, 'bid' => 0.4, 'priority' => 4),
    array('id' => 104, 'bid' => 0.4, 'priority' => 4),
    array('id' => 105, 'bid' => 0.3, 'priority' => 5),
    array('id' => 106, 'bid' => 0.3, 'priority' => 5),
    array('id' => 107, 'bid' => 0.2, 'priority' => 5),
    array('id' => 108, 'bid' => 0.7, 'priority' => 5),
    array('id' => 108, 'bid' => 0.1, 'priority' => 4)
);
  • 写回答

7条回答 默认 最新

  • duanpa1980 2013-05-07 08:27
    关注

    Based on idea @Hugo's answer about using random weight:

    $array = array(
        array('id' => 101, 'bid' => 0.5, 'priority' => 5),
        array('id' => 102, 'bid' => 0.4, 'priority' => 4),
        array('id' => 103, 'bid' => 0.4, 'priority' => 4),
        array('id' => 104, 'bid' => 0.4, 'priority' => 4),
        array('id' => 105, 'bid' => 0.3, 'priority' => 5),
        array('id' => 106, 'bid' => 0.3, 'priority' => 5),
        array('id' => 107, 'bid' => 0.2, 'priority' => 5),
        array('id' => 108, 'bid' => 0.7, 'priority' => 5),
        array('id' => 108, 'bid' => 0.1, 'priority' => 4)
    );
    function cmp(&$a, &$b) {                                     # notice the use of & in function signature
        if ($a['bid'] - $b['bid']) {
            return $a['bid'] - $b['bid'] > 0 ? 1 : -1;           # bid is different, sort using bid
        } else if ($a['priority'] - $b['priority']) {
            return $a['priority'] - $b['priority'] > 0 ? 1 : -1; # priority is different, sort using priority
        } else {
            if (isset($a['rw']) == false) {
                $a['rw'] = rand(1, 100);                         # assign random tie breaker
            } 
            if (isset($b['rw']) == false) {
                $b['rw'] = rand(1, 100);                         # assign random tie breaker
            } 
            if ($a['rw'] - $b['rw']) {
                return $a['rw'] - $b['rw'] > 0 ? 1 : -1;         # sort using random weight
            } else {
                return 0;
            }
        }
    }
    usort($array, 'cmp');
    var_dump($array);
    

    Output

    array(9) {
    [0]=>array(3) {["id"]=>int(108) ["bid"]=>float(0.1) ["priority"]=>int(4)}
    [1]=>array(3) {["id"]=>int(107) ["bid"]=>float(0.2) ["priority"]=>int(5)}
    [2]=>array(4) {["id"]=>int(106) ["bid"]=>float(0.3) ["priority"]=>int(5) ["rw"]=>int(70)}
    [3]=>array(4) {["id"]=>int(105) ["bid"]=>float(0.3) ["priority"]=>int(5) ["rw"]=>int(73)}
    [4]=>array(4) {["id"]=>int(103) ["bid"]=>float(0.4) ["priority"]=>int(4) ["rw"]=>int(29)}
    [5]=>array(4) {["id"]=>int(104) ["bid"]=>float(0.4) ["priority"]=>int(4) ["rw"]=>int(67)}
    [6]=>array(4) {["id"]=>int(102) ["bid"]=>float(0.4) ["priority"]=>int(4) ["rw"]=>int(80)}
    [7]=>array(3) {["id"]=>int(101) ["bid"]=>float(0.5) ["priority"]=>int(5)}
    [8]=>array(3) {["id"]=>int(108) ["bid"]=>float(0.7) ["priority"]=>int(5)}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办