Javascript-how the Olympic medal is determined by the order of gold, silver, and copper (for details, refer to the topic)

Source: Internet
Author: User
The data structure is probably like this {code ...} the sorting rule is: First compare to the gold medal, the gold medal is consistent than the silver medal, the silver medal is consistent than the bronze medal, the bronze medal is consistent than the id, the id is the initial array number score three numbers represent the gold, respectively, the number of silver and copper requires less built-in functions. it is best to use php... the data structure is like this.

$ Arr = [['name' => 'country 1', 'Score '=> [, 5], ['name' => 'country 2 ', 'score '=> [, 5], ['name' => 'country 3', 'Score' => [, 5], ['name' => 'country 4', 'Score '=> [10, 7, 9],];

The sorting rule is: First compare to the gold medal, the gold medal is consistent than the silver medal, the silver medal is consistent than the bronze medal, the bronze medal is consistent than the id, id is the initial array number

The three numbers in score indicate the quantity of gold, silver, and copper respectively.

Built-in functions are rarely required. it is best to use php for implementation.

Reply content:

The data structure is like this.

$ Arr = [['name' => 'country 1', 'Score '=> [, 5], ['name' => 'country 2 ', 'score '=> [, 5], ['name' => 'country 3', 'Score' => [, 5], ['name' => 'country 4', 'Score '=> [10, 7, 9],];

The sorting rule is: First compare to the gold medal, the gold medal is consistent than the silver medal, the silver medal is consistent than the bronze medal, the bronze medal is consistent than the id, id is the initial array number

The three numbers in score indicate the quantity of gold, silver, and copper respectively.

Built-in functions are rarely required. it is best to use php for implementation.

There are three numbers representing the number of medals. do you have to explain at least what the sorting rules look like?

Is the total number of medals? Respectively according to gold and silver copper? Or what strange arrangement and combination?

There are many ways to do this. the lazy way is to sort the gold medal by four times, select the same gold medal, and then sort it by the silver medal in the same way ...... And so on.

If you want to sort gold, silver, and copper numbers in a single order, you can simply turn them into numbers.
For example, your array can become like this --

[010007005001,010009005002,011007005003,010007009004]

The rule is very simple. the numbers of gold, silver, and copper are filled in three places, and then directly spliced. Finally, you can sort these numbers directly and set them in place at a time.

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


  Array ('gold medal '=> 8, 'Silver medal' => 3, 'Bronze Medal '=> 6,), 'Russia' => array ('gold medal '=> 3, 'silver '=> 6, 'bronze' => 3,), 'u.s. '=> array ('gold medal' => 6, 'Silver medal '=> 8, 'bronze '=> 8,), 'Australian' => array ('gold medal '=> 4, 'Silver medal' => 0, 'Bronze Medal '=> 4 ,), 'Italy '=> array ('golde' => 3, 'silde' => 4, 'Bronze' => 2 ,),); // implement ORDER BYforeach ($ arr as $ k => $ v) {$ sort ['golde'] [$ k] = $ v ['golde']; $ sort ['siliver'] [$ k] = $ v ['siliver']; $ sort ['bronz'] [$ k] = $ v ['bronz'];} array_multisort ($ sort ['gold Med'], SORT_DESC, $ sort ['Silver '], SORT_DESC, $ sort ['Bronze'], SORT_DESC, $ arr); var_export ($ arr );

For the python version, assume that the number of each prize in each country cannot exceed 999.

Arr = [{'name': 'Country 1', 'Score ': [10, 7, 5]}, {'name': 'Country 2', 'Score ': [, 5]}, {'name': 'Country 3', 'Score ': [, 5]}, {'name': 'Country 4 ', 'score ': [10, 7, 9]},] print sorted (arr, key = lambda: '% 03d % 03d % 03d' % tuple (a ['score ']), reverse = True)

It is best to separate scores during design.
Design a class (including country, gold, silver, copper) to implement the Comparable interface and implement the compare method according to the rules you describe. Isn't that easy?

Gold + Silver + bronze + id to form a number and then sort it directly, for example, id13-[10, 2, 20] to form 10022013, and then sort it directly based on this number.

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.