开发者

JavaScript - The internal JavaScript sort method sorts numeric data - True or False? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago. 开发者_如何学运维

The internal JavaScript sort method sorts numeric data - True or False?


If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. For example, "80" comes before "9" in lexicographic order, but in a numeric sort 9 comes before 80.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

So the answer is False.


It seems it doesn't sort very well...

[3,5,1,5,10,0,99,10,12].sort()
[0, 1, 10, 10, 12, 3, 5, 5, 99]  // result

But you can easily make it sort ok:

[3,5,1,5,10,0,99,10,12].sort(function(a,b) {return parseInt(a) > parseInt(b)})
[0, 1, 3, 5, 5, 10, 10, 12, 99] // result
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜