dongnai2804 2016-11-27 20:52
浏览 39
已采纳

基于多个值对数组进行排序

I need to sort an array of objects based on the objects properties. Basically if you take a look at this fixture table and note "Liverpool FC" and "Manchester City FC" you can see that they have the same amount of points, so they are ordered based on the other values being higher.

Now if you take a look at mine i'm only ordering based on the points, i'm not sure how to order this based on multiple values.

The data is stored within an instance of a class, this instance is stored within an Array() with the Key being the team name.

Below is the class which managed the data, how can I re-arrange the array so that the objects are in order the way the first link has them ordered?

 class Calc {
    private $config;
    public $win = 0, $draw = 0, $loss = 0, $goalFor = 0, $goalConc = 0;

    public function __construct($payload = array(0,0)) {
      // Load config file
      $this->config = parse_ini_file('config.ini', true);

      // Add wins, losses, draws, goals for and goal conceived
      $this->addData($payload);
    }

    // Linked data updated, ammend values
    public function calcPlays() {
      return 0 + $this->win + $this->draw + $this->loss;
    }
    public function calcPoints() {
      // Add 0 to ensure value is an int
      return $this->win * ($this->config['winPoints']) + ($this->draw * $this->config['drawPoints']) + ($this->loss * $this->config['lossPoints']);
    }
    public function calcGoalDifference() {
      return ($this->goalFor - $this->goalConc);
    }

    public function addData($data) {
      // Append goal data
      $this->goalFor += $data[0];
      $this->goalConc += $data[1];

      // Win, Loss or Draw
      if ($data[0] > $data[1]) { $this->win++;} elseif
      ($data[0] < $data[1]) { $this->loss++;} elseif
      ($data[0] == $data[1]) { $this->draw++;}
    }
  }

Edit:

My data is now everywhere:

1   Burnley FC  13  4   2   7   12  21  -9  14
2   Leicester City FC   13  3   4   6   16  22  -6  13
3   Crystal Palace FC   13  3   2   8   21  26  -5  11
4   Swansea City FC 13  2   3   8   16  26  -10 9
5   Arsenal FC  13  8   4   1   28  13  15  28

I'm assuming my checks are the wrong way around, I was assuming it would check if $a is bigger than or equal to $b, if so then return true, if not move on to the next check?

The code:

// Sort teams by points
uasort($teamData, function($a, $b) {
  if ($a->calcPoints() < $b->calcPoints() && $a->calcPoints() !== $b->calcPoints()) {
    return true;
  } elseif ($a->calcGoalDifference() < $b->calcGoalDifference() && $a->calcGoalDifference() !== $b->calcGoalDifference()) {
    return true;
  } elseif($a->goalConc < $b->goalConc) {
    return true;
  }
  return false;
});
  • 写回答

2条回答 默认 最新

  • dongyinglan8707 2016-11-27 21:07
    关注

    You can use usort and write a function that compares the different values and sorts them accordingly.

    Something along the lines of:

    uasort($teamData, function ($a, $b)
    {
        if ( $a->calcPoints() < $b->calcPoints() )
        {
            return 1;
        }
        elseif ( $a->calcPoints() <= $b->calcPoints() && $a->calcGoalDifference() < $b->calcGoalDifference() )
        {
            return 1;
        }
        elseif ( ($a->calcPoints() <= $b->calcPoints() && $a->calcGoalDifference() <= $b->calcGoalDifference()) && $a->goalConc < $b->goalConc )
        {
            return 1;
        }
    
        return 0;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据