Words don't say much, directly on the code
HTML Source:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
JS Source code
//JavaScript DocumentvarMyarr=[];$(function(){ //Click the Sort button to trigger the event$ ("#click"). Click (function() {insert_mine (); }) Document.onkeydown=function(event) {vare=event| | window.event| | Arguments.callee.caller.arguments[0]; if(e&&e.keycode==13&&$ ("#content"). Is (": Focus") ) {insert_mine (); } } })varInsert_mine=function(){ varcontent=$ ("#content"); if(!content.val ()) {Alert ("Please enter what you want to sort!"} ");return false;}; varArr=content.val (). Split (","); for(vari=0;i<arr.length;i++) { if(IsNaN (Arr[i])) {Alert ("The entry has an illegal number, the program has exited!") ");return false;} Myarr=arr_solve (Myarr,arr[i]); } alert (The sort ends. "); $("#conclude"). Val (myarr.tostring ()); Myarr=[];}varArr_solve=function(arr1,i) {Debugger; if(arr1.length==0) {arr1[0]=i; } Else { for(vark=0;k<arr1.length;k++) { if(parseint (Arr1[k]) >=parseint (i) &&k<arr1.length) {//The previous index doesn't move, it's the index behind it . //arr[k]=i; for(varj=arr1.length;j>k;j--) {Arr1[j]=arr1.slice (0) [j-1]; } Arr1[k]=i; Console.log (Arr1.tostring ()); returnarr1; } Else { if(arr1.length==k+1) {Arr1[arr1.length]=i; Console.log (Arr1.tostring ()); returnarr1; } } } } console.log (Arr1.tostring ()); returnarr1; } Input rule: Enter a comma-separated number in the input box
Output rule: Sort results in ascending order from small to large
Operation Result:
It is important to note that the exchange of elements in an array cannot be directly assigned to the slice method using the array's own.
Note: Running this program requires importing the jquery library