标题:求教sort函数的问题
只看楼主
devilqin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-6-13
结帖率:0
已结贴  问题点数:20 回复次数:3 
求教sort函数的问题

        var queue = ["red","green","yellow","purple"];
        queue.splice(1, 2 , "white,black,blue");
        alert(queue.sort().toString()); //output  "purple,red,white,black,blue",这里输出为啥不是按首字母排列?

[ 本帖最后由 devilqin 于 2012-6-13 16:21 编辑 ]
搜索更多相关主题的帖子: white black purple 字母 
2012-06-13 16:19
aspic
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:51
帖 子:2258
专家分:8050
注 册:2008-2-18
得分:10 
按照 ASCII 字符顺序进行升序排列
2012-06-14 22:12
aspic
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:51
帖 子:2258
专家分:8050
注 册:2008-2-18
得分:0 
<script>
var queue = ["red","white","black","blue","green","yellow","purple"];
//queue.splice(1, 2 , "white,black,blue");
alert(queue.sort().toString()); //output  "black,blue,green,purple,red,white,yellow"
</script>
2012-06-14 22:17
hibill
Rank: 2
等 级:论坛游民
帖 子:3
专家分:20
注 册:2012-6-4
得分:10 
回复 楼主 devilqin
因为的的splice的第三个参数错了,
你那样的写法是将"white,black,blue"看成一个整体,
如果写成这样:
queue.splice(1, 2 , "white","black","blue");
就不会发生排序错误了;
 var queue = ["red","green","yellow","purple"];
 queue.splice(1, 2 , "white","black","blue");
 alert(queue.sort().toString());
//black,blue,purple,red,white
2012-06-16 16:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-371467-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.184525 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved