JavaScript fun: weight loss club

Source: Internet
Author: User
Mr. Wang is always worried recently because a weight list of all members will be released over the past few days, and he is the fattest every time. My friend Tom and I are both members of the "Weight Loss Fitness Club.

Mr. Wang is always worried recently because a weight list of all members will be released over the past few days, and he is the fattest every time.

I am also a member of the drafting list, so I told him: "Don't worry, my friend, I will help you change the list order ".

After thinking about it, I thought it would be better to sort it in this order: the sum of the numbers of each weight, and the sum of the numbers, from small to large.

For example, 99 kg, the sum of its digits is 18, that is, the "weight" is 18.

For example, the weight of 100 is 1, so it is placed before 99kg.

The task is as follows:

Given the weight loss club member list string, can you sort it by weight and return it?

Example:

"56 65 74 100 99 68 86 180 90"  => "100 180 90 56 65 74 68 86 99"

When two numbers have the same weight, for example, 180 and 90, then 180 is in the first place. Now we will follow the lexicographically.


All numbers are positive, and the list may be empty.

To be honest, I am a little sweaty when I see this question ~ After all, I am also a fat man.

It is obviously not convenient to directly operate on strings. split them into arrays!

Sort the array according to the following rules:

1. The higher the weight, the smaller the weight, and the higher the ranking.

2. When the weights are the same, sort them in Lexicographic Order.

This method is required to calculate the weight, that is, to calculate the sum of digits:

function eachDigitSum(num){      var sum = 0;      num = num - 0;      while(num > 0){          sum += num % 10;          num = parseInt(num / 10);      }      return sum;  }

Next, we need this method to compare the Lexicographic Order:

function compareASCII(a,b){      var i=0;      while(true){          var c1 = a.charCodeAt(i);          var c2 = b.charCodeAt(i);          if(!c1){              return -1;          }          if(!c2){              return 1;          }          if(c1 < c2){              return -1;          }          if(c1 > c2){              return 1;          }          i++;      }  }

In fact, this method has an alternative method in native JS:

String.prototype.localeCompare

By default, this is in the Lexicographic Order.


Finally, sort and merge the strings.

function orderWeight(str) {      return str.split(" ").sort(function(a,b){          var n1 = eachDigitSum(a);          var n2 = eachDigitSum(b);          if(n1 > n2){              return 1;          }          else if(n1 < n2){              return -1;          }          else{              return compareASCII(a,b);          }      }).join(" ");  }

The above is JavaScript fun: the content of the weight loss club. For more information, please follow the PHP Chinese Network (www.php1.cn )!

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.