Javascript sort() with concat()
Javascript sort() with concat()
为什么在concat之后不按数字顺序排序?
var hege = [34, 12];
var stale = [1, 78, 8, 4];
var children = hege.concat(stale).sort(); //1,12,34,4,78,8
http://jsfiddle.net/6kN5H/.sort
方法按字母顺序对元素进行排序
.sort(function(a,b){return a - b})
var children = hege.concat(stale).sort(function(a,b){return a - b}); //1,4,8,12,34,78
检查这个提琴
试试这个
hege.concat(stale).sort(function(a,b){return (+a)-(+b)})
这里我们正在转换number &按数字排序。
因为默认情况下javascript sort按字符串排序,所以你必须指定你想要如何排序
function myFunction() {
var hege = [34, 12];
var stale = [1, 78, 8, 4];
var children = hege.concat(stale).sort((hege, stale) => hege>stale);
document.getElementById("demo").innerHTML= children;
}
myFunction();
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- 如何在包装功能中咕哝concat
- How to declare a Map containing certain properties with flow
- javascript concat
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- array.push.apply to implment 'concat' with explain
- Javascript sort() with concat()