码迷,mamicode.com
首页 > 编程语言 > 详细

js中Array.sort()对纯数字排序错误

时间:2017-12-17 23:48:15      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:gpo   fun   编码   array   div   turn   function   .so   方法   

在js中对纯数字的的数组用sort()函数排序时,会发现的他的排序是错误的,这是因为它采用的unicode编码导致的

arr=[2,6,3,4,11,1];
arr.sort();
console.log(arr);

返回结果:[1, 11, 2, 3, 4, 6]

解决方法:

arr.sort(function (a,b) {
return a-b;
});
console.log(arr);

返回结果:[1, 2, 3, 4, 6, 11]

 

js中Array.sort()对纯数字排序错误

标签:gpo   fun   编码   array   div   turn   function   .so   方法   

原文地址:http://www.cnblogs.com/Aizongbin/p/8053580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!