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

时间:2022-02-08 15:08:23
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="JavaScript" type="text/javascript"> function addnew()
{
c = prompt("请输入内容!","");//列
way = prompt("请输入排序方式(升 1/降 0)!","");//列
if (c == null)
{
return false;
}
else if (c.replace(/^\s+|\s+$/g,"") == "")
{
alert("输入内容为空!");
return false;
}
// alert(c);
// alert(way);
}
addnew();
var arr1 = c.toString().split(" ");
if(way==1)
{
arr1.sort();
for(var i=0;i<arr1.length;i++)
document.write(arr1[i]+" ");
}
//(降序)
else
{
arr1.sort(function (a,b){return a>b?-1:1});
for(var i=0;i<arr1.length;i++)
document.write(arr1[i]+" ");
} </script>
</body> <title>无标题文档</title>
</head> <body>
</body>
</html>

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/812996