javascript - For Olympic medals, how to determine the national ranking according to the order of gold, silver and bronze (see the title for details)

WBOY
Release: 2016-10-10 11:56:10
Original
2220 people have browsed it

The structure of the data is roughly like this

<code> $arr = [
          ['name'=>'国家一','score'=>[10,7,5]],
          ['name'=>'国家二','score'=>[10,9,5]],
          ['name'=>'国家三','score'=>[11,7,5]],
          ['name'=>'国家四','score'=>[10,7,9]],
        ];
</code>
Copy after login
Copy after login

The sorting rule is, gold medals are compared first, gold medals are always better than silver medals, silver medals are always better than bronze medals, bronze medals are always better than id, and id is the initial array sequence number

The three numbers in the score represent the amount of gold, silver and copper respectively

Those that require less use of built-in functions are best implemented in PHP

Reply content:

The structure of the data is roughly like this

<code> $arr = [
          ['name'=>'国家一','score'=>[10,7,5]],
          ['name'=>'国家二','score'=>[10,9,5]],
          ['name'=>'国家三','score'=>[11,7,5]],
          ['name'=>'国家四','score'=>[10,7,9]],
        ];
</code>
Copy after login
Copy after login

The sorting rule is, gold medals are compared first, gold medals are always better than silver medals, silver medals are always better than bronze medals, bronze medals are always better than id, and id is the initial array sequence number

The three numbers in the score represent the amount of gold, silver and copper respectively

Those that require less use of built-in functions are best implemented using PHP

There are three numbers that represent the number of medals. Can you at least tell me what the sorting rules are?

Is it the total number of medals? According to gold, silver and copper respectively? Or some strange permutation?


There are many ways. The lazy way is to compare four times directly. First sort by gold medal, select the ones with the same gold medal, and then sort by silver medal among the same ones... and so on

If you want to sort them at once, just change the gold, silver, and copper serial numbers into a number to sort them.
For example, your array can become like this——

<code>[010007005001,010009005002,011007005003,010007009004]</code>
Copy after login

The rules are very simple. Fill in the gold, silver, and copper serial numbers to three digits, then directly splice them together. Finally, just sort the group of numbers directly, and it will be done in one go.

PHP array_multisort implements sorting by multiple fields like SQL ORDER BY.
For example, the Olympic medal list is sorted in descending order by the number of gold medals, silver medals, and bronze medals.

<code><?php
header('Content-Type: text/plain; charset=utf-8');
$arr = array(
    '中国' => array(
        '金牌' => 8,
        '银牌' => 3,
        '铜牌' => 6,
    ),
    '俄罗斯' => array(
        '金牌' => 3,
        '银牌' => 6,
        '铜牌' => 3,
    ),
    '美国' => array(
        '金牌' => 6,
        '银牌' => 8,
        '铜牌' => 8,
    ),
    '澳大利亚' => array(
        '金牌' => 4,
        '银牌' => 0,
        '铜牌' => 4,
    ),
    '意大利' => array(
        '金牌' => 3,
        '银牌' => 4,
        '铜牌' => 2,
    ),

);
// 实现 ORDER BY
foreach($arr as $k => $v) {
    $sort['金牌'][$k] = $v['金牌'];
    $sort['银牌'][$k] = $v['银牌'];
    $sort['铜牌'][$k] = $v['铜牌'];
}
array_multisort(
    $sort['金牌'], SORT_DESC, 
    $sort['银牌'], SORT_DESC, 
    $sort['铜牌'], SORT_DESC, 
    $arr);
var_export($arr);</code>
Copy after login

Let’s make a python version, assuming that the number of each type of medals in each country will not exceed 999.

<code>arr = [
    {'name':'国家一','score':[10,7,5]},
    {'name':'国家二','score':[10,9,5]},
    {'name':'国家三','score':[11,7,5]},
    {'name':'国家四','score':[10,7,9]},
]

print sorted(arr, key=lambda a: '%03d%03d%03d' % tuple(a['score']), reverse=True)</code>
Copy after login

score is best separated when designing.
Design a class (including country, gold, silver, and bronze numbers), implement the Comparable interface, and implement the compare method according to the rules you described. Isn't it simple?

Gold Medal + Silver Medal + Bronze Medal + ID form a number and then sort it directly. For example, id13-[10, 2, 20] forms 10022013, and then sort it directly according to this number

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template