Array de-reordering statistics sort

Source: Internet
Author: User

Requirements are: Count the number of times and order according to requirements;

Ideas:

The first step: stitching strings, returning an array of strings;

The second step: Query the array, go to the weight and count the number;

Step three: Sort by using the sort () function;

Fourth step: Join () function. Put the elements of the array into the string, it is really around, learn the program is not the way, more practice.

<!doctype Html>"Utf-8"><title> array de-reordering statistics sort </title><style>Body {text-Align:center;}</style><script>window.onload=function () {varObtn=document.getelementbyid ('BTN1'); varOtext1=document.getelementbyid ('txt1'); varOtext2=document.getelementbyid ('txt2'); Obtn.onclick=function () {varjson={}; varArr=otext1.value.split (' ');  for(varI=0; i<arr.length; i++)        {            varValue=Arr[i]; if(Json[value]) {Json[value]++; }            Else{Json[value]=1; }        }                //alert (json.stringify (JSON));                        /*[{name:i, Num:json[i]}, {name:i, Num:json[i]}, {name:i, num: Json[i]}]*/        varArr2=[];  for(varIinchJSON)        {Arr2.push ({name:i, num:json[i]}); }        //Console.log (ARR2);Arr2.sort (function (j1,j2) {returnj2.num-J1.num;                }); vararr3=[];  for(varI=0; i<arr2.length;i++)        {            varjson=Arr2[i]; Arr3.push (json['name']+':'+json['Num']); } otext2.value=arr3.join ('\ n'); };}; function Findinarr (arr, obj) { for(varI=0; i<arr.length; i++)    {        if(arr[i]==obj) {            return true; }    }        return false;}</script>Enter the original text (space separated):<br><textarea style="width:300px; height:200px;"Id="txt1"> Clothes sneakers Basketball shoes running shoes casual shoes canvas shoes football shoes clothes clothes clothes running shoes running shoes running shoes running shoes casual shoes canvas shoes casual shoes canvas shoes casual shoes Canvas shoes </textarea><br><br><in Put type="Button"Value="Change"Id="BTN1"/><br><br>here output:<br><textarea style="width:300px; height:200px;"Id="txt2"></textarea></body>

Array de-reordering statistics sort

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.