使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>test3_2</title>
- <script type="text/javascript" src="sort.js">
- function input()
- {
- do
- {
- c = prompt("请输入英文句子!","hello");
- if(c == null)
- {
- return false;
- }
- if(c.replace(/^\s+|\s+$/g,"") == "")
- {
- alert("输入内容为空!");
- }
- }
- while (c.replace(/^\s+|\s+$/g,"") == "" && c != null);
- way = prompt("请输入排序方式(升 1/降 0)!","1");
- // alert(c);
- // alert(way);
- }
- input();
- var arr1 = c.toString().split("");
- var d1 = arr1.toString().replace(/,/g,"\r\n");//去掉数组中的逗号
- document.write("<center><label id=label1>原句:</label><input id=a type=text name=111 value size=30 maxlength=30 /></center>");
- document.getElementById("a").value=d1;
- document.write("<br />");
- //升序
- if(way=="1")
- {
- //arr1.sort();
- var arr2=arr1.sort();
- var d2 = arr2.toString().replace(/,/g,"\r\n");
- document.write("<center><label id=label2>升序:</label><input id=b type=text name=222 value size=30 maxlength=30 /></center>");
- document.getElementById("b").value=d2;
- }
- //降序
- else
- {
- var arr3=arr1.sort(function (a,b){return a>b?-1:1});
- var d3 = arr3.toString().replace(/,/g,"\r\n");
- document.write("<center><label id=label3>降序:</label><input id=b type=text name=222 value size=30 maxlength=30 /></center>");
- document.getElementById("b").value=d3;
- }
- </script>
- </head>
- <body>
- </body>
- </html>
效果如下:
转载于:https://blog.51cto.com/outdoxl/835369