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