js汉字排序,拼音排序,排序
我们很多同学没有用过javascript的排序吧,作为一门强大如斯,称霸web端的语言怎么能没有排序呢,js排序和其他语言类似 使用 sort即可。这里有个demo,拷贝粘贴即可运行:
<html>
<head>
<meta charset='utf8'/>
</head>
<style>
textarea{width: 100%; overflow-y: auto; word-wrap: normal;}
input{width: 500px;height:30px;}
</style>
<script type="text/javascript">
function startSort(){
var a=document.getElementById('s').value;
a=a.split(',');
a.sort();
document.getElementById('r1').value=a;
a.sort(function(a,b){return a.localeCompare(b)});//汉字拼音排序方法
document.getElementById('r2').value=a;
}
</script>
<p>包含汉字的字符串数组(用逗号","隔开):</p>
<textarea id="s" rows="10">周杰伦,b土,baidu,alibaba,百度,阿里巴巴,那英,google,汪峰,庾澄庆,古惑仔,李白,apple,杜甫</textarea>
<p style="text-align: center"><input type="button" value="排序测试" onclick="startSort()" /></p>
<p>默认排序结果:</p>
<textarea id="r1" rows="10"></textarea>
<p>汉字拼音顺序排序结果:</p>
<textarea id="r2" rows="10"></textarea>
</html>
运行结果如下:

解决了排序了吧.