使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出

 

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3.  <head> 
  4.   <title>test3_2</title> 
  5.   <script type="text/javascript" src="sort.js"> 
  6.     function input()  
  7.     { 
  8.         do 
  9.         { 
  10.             c = prompt("请输入英文句子!","hello"); 
  11.                 if(c == null)     
  12.                 { 
  13.                     return false;   
  14.                 }    
  15.                 if(c.replace(/^\s+|\s+$/g,"") == "")     
  16.                 {  
  17.                     alert("输入内容为空!");  
  18.                 } 
  19.         } 
  20.         while (c.replace(/^\s+|\s+$/g,"") == "" && c != null);      
  21.  
  22.         way = prompt("请输入排序方式(升 1/降 0)!","1"); 
  23.         //  alert(c);  
  24.         // alert(way);  
  25.     }  
  26.     input();  
  27.     var arr1 = c.toString().split("");  
  28.     var d1 = arr1.toString().replace(/,/g,"\r\n");//去掉数组中的逗号 
  29.     document.write("<center><label id=label1>原句:</label><input id=a type=text name=111 value size=30 maxlength=30 /></center>"); 
  30.     document.getElementById("a").value=d1
  31.     document.write("<br />"); 
  32.     //升序 
  33.     if(way=="1")  
  34.     { 
  35.         //arr1.sort(); 
  36.         var arr2=arr1.sort();  
  37.         var d2 = arr2.toString().replace(/,/g,"\r\n"); 
  38.         document.write("<center><label id=label2>升序:</label><input id=b type=text name=222 value size=30 maxlength=30 /></center>"); 
  39.         document.getElementById("b").value=d2
  40.     }  
  41.     //降序  
  42.     else  
  43.     {  
  44.         var arr3=arr1.sort(function (a,b){return a>b?-1:1}); 
  45.         var d3 = arr3.toString().replace(/,/g,"\r\n"); 
  46.         document.write("<center><label id=label3>降序:</label><input id=b type=text name=222 value size=30 maxlength=30 /></center>"); 
  47.         document.getElementById("b").value=d3
  48.     }  
  49.   </script> 
  50.  </head> 
  51.  <body> 
  52.    
  53.  </body> 
  54. </html> 

效果如下: