duanliyi5997 2011-04-29 08:43
浏览 63
已采纳

PHP:按多个属性对锦标赛进行排序

I am developing a soccer tournament management system and I'm stuck with a problem. I have to show to the user round teams ranking based on different attributes. I'm right now considering the following order:

  • team points ( 3*won match + 1*tie match )
  • team confrontational( not sure if this is the right word but I mean matches between same teams, i.e. if two teams have the same points but one was defeated by the other, the winning one will be above the loser one )
  • goal difference (goal scored-goal taken)
  • goal scored
  • alphabetical order

Data to build this model are retrieved through different database query and the structure I'm using right now to save them is an array of objects like this:

  • teamId
  • played matches
  • won matches
  • lost matches
  • tie matches
  • points
  • goal scored
  • goal taken
  • goal difference
  • teams that were defeated current one = array(teamId1,teamId2...)
  • teams that defeated current one= array(teamId1,teamId2...)
  • teams that had a tie with current one = array(teamId1,teamId2...)

For most sorting attributes I can use php array_multisort function and get the job done but the issue is about team confrontational. As an example let's say I have this situation:

  • Team A - 3 points - won against D
  • Team B - 3 points - won against D
  • Team C - 3 points - won against A
  • Team D - 0 points - won against D

In this case I have that team C should stay above A since C defeated A while I should check goal difference(next attribute in the order) to determine B position compared to B and C. I'm trying to develop an algorithm able to sort this issue with any attribute configuration but had no luck so far. Anyone has any suggestion? Thank you very much and forgive me if I were not clear

  • 写回答

2条回答 默认 最新

  • douweng7308 2011-05-01 23:52
    关注

    what you call team confrontational is actually a new table calculated for the teams that are tying with the same points using only the games between them. This problem evolves into a particularly nasty situation when you have 3 or more teams tied in points, in which case you cannot sort the position table using one to one comparisons (which is what all sorting algorithms do) as you need to know the other teams tied to create your sub-table. You would need to create a function that given a list of matches and a list of teams generates your position table for those teams, sorted by caring only for points. Before returning the position table check if there are groups with the same points and replace that section of the table with the result of a recursive call with all the matches but only the tied group teams. Be sure to stop the recursion when the whole table is tied or else you get infinite loop, in that case apply global goal diff, goals scored and alphabetical.

    PD: I tried solving this problem before and gave up after realizing I was dealing with non comparable sets. If you actually implement it I would be very happy to check out the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来