JavaScript-Olympic medals, how to achieve according to the Order of gold and silver copper determine the country ranked (see topic)

Source: Internet
Author: User
The structure of the data is probably like this

 $arr = [          ['name'=>'国家一','score'=>[10,7,5]],          ['name'=>'国家二','score'=>[10,9,5]],          ['name'=>'国家三','score'=>[11,7,5]],          ['name'=>'国家四','score'=>[10,7,9]],        ];

The rule of sorting is that the first is better than the gold medal, the gold medal is more consistent than the bronze, the bronze is consistent than the Id,id is the initial array ordinal

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

Requires less use of built-in functions, preferably PHP implementation

Reply content:

The structure of the data is probably like this

 $arr = [          ['name'=>'国家一','score'=>[10,7,5]],          ['name'=>'国家二','score'=>[10,9,5]],          ['name'=>'国家三','score'=>[11,7,5]],          ['name'=>'国家四','score'=>[10,7,9]],        ];

The rule of sorting is that the first is better than the gold medal, the gold medal is more consistent than the bronze, the bronze is consistent than the Id,id is the initial array ordinal

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

Requires less use of built-in functions, preferably PHP implementation

There are three numbers representing the number of medals, and you at least say what sort of rules it looks like.

Is the total number of medals? In accordance with gold and silver copper? Or what weird permutations?

Way a lot of, more lazy way is directly than four times, first in accordance with the gold medal sort, select the same gold medal, and then in the same inside according to the silver sort ... And so on.

If you want to sort it all at once, then you can sort the gold and silver numbers into a number.
For example, this array of yours can be turned into this--

[010007005001,010009005002,011007005003,010007009004]

The rule is simple, gold and Silver copper serial number filled to three bits, and then directly splicing up, finally directly to this group of numbers to sort on the line, once in place.

PHP Array_multisort implements sorting by multiple fields like SQL ORDER by.
such as the Olympic medal table, in turn by the gold medal, silver medal, the number of bronze medals in descending order.


  
    array(        '金牌' => 8,        '银牌' => 3,        '铜牌' => 6,    ),    '俄罗斯' => array(        '金牌' => 3,        '银牌' => 6,        '铜牌' => 3,    ),    '美国' => array(        '金牌' => 6,        '银牌' => 8,        '铜牌' => 8,    ),    '澳大利亚' => array(        '金牌' => 4,        '银牌' => 0,        '铜牌' => 4,    ),    '意大利' => array(        '金牌' => 3,        '银牌' => 4,        '铜牌' => 2,    ),);// 实现 ORDER BYforeach($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);

A python version, assuming that each country has no more than 999 medals per medal.

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)

Score should be separated at design time.
Design a class (including country, gold, silver, copper number), implement comparable interface, implement compare method according to the rules you describe. Isn't that easy?

Gold + silver + Bronze +id compose a number and then sort directly, such as id13-"10,2,20" Make up 10022013, and then sort directly according to this number

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.