How to sort an array of integers correctly
试图从我知道只包含整数的数组中获取最高和最低值似乎比我想象的要困难。
1 2 3 | var numArray = [140000, 104, 99]; numArray = numArray.sort(); alert(numArray) |
我希望这个能显示
有没有办法让排序函数对整数值进行实际排序?
默认情况下,sort方法按字母顺序对元素排序。要进行数字排序,只需添加一个处理数字排序的新方法(SortNumber,如下所示)-
1 2 3 4 5 6 7 | function sortNumber(a,b) { return a - b; } var numArray = [140000, 104, 99]; numArray.sort(sortNumber); alert(numArray.join(",")); |
编辑:使用ES6箭头函数:
1 2 | numArray.sort((a, b) => a - b); // For ascending sort numArray.sort((a, b) => b - a); // For descending sort |
在以上所有答案的基础上,它们也可以用如下一行来完成:
1 2 3 4 5 6 7 8 9 | var numArray = [140000, 104, 99]; // ES5 numArray = numArray.sort(function (a, b) { return a - b; }); // ES2015 numArray = numArray.sort((a, b) => a - b); //outputs: 99, 104, 140000 |
array.sort默认情况下执行词典排序,对于数字排序,提供自己的函数。下面是一个简单的例子:
1 2 3 4 5 6 | function compareNumbers(a, b) { return a - b; } numArray.sort(compareNumbers); |
还要注意,排序工作"到位",不需要分配任务。
这个答案相当于一些现有的答案,但是ecmascript 6 arrow函数提供了一个更紧凑的语法,允许我们在不牺牲可读性的情况下定义一个内联排序函数:
1 | numArray = numArray.sort((a, b) => a - b); |
现在大多数浏览器都支持它。
排序函数行为如此怪异的原因
从文档中:
[...] the array is sorted according to each character's Unicode code point
value, according to the string conversion of each element.
如果您打印数组的unicode点值,那么它将被清除。
1 2 3 4 5 | console.log("140000".charCodeAt(0)); console.log("104".charCodeAt(0)); console.log("99".charCodeAt(0)); //Note that we only look at the first index of the number"charCodeAt( 0 )" |
这将返回:"49,49,57"。
1 2 3 | 49 (unicode value of first number at 140000) 49 (unicode value of first number at 104) 57 (unicode value of first number at 99) |
现在,因为140000和104返回了相同的值(49),所以它削减了第一个索引并再次检查:
1 2 3 4 | console.log("40000".charCodeAt(0)); console.log("04".charCodeAt(0)); //Note that we only look at the first index of the number"charCodeAt( 0 )" |
1 2 | 52 (unicode value of first number at 40000) 40 (unicode value of first number at 04) |
如果我们将其分类,那么我们将得到:
1 2 | 40 (unicode value of first number at 04) 52 (unicode value of first number at 40000) |
所以104在14万之前。
所以最终结果是:
1 2 3 | var numArray = [140000, 104, 99]; numArray = numArray.sort(); console.log(numArray) |
结论:
为了正确排序,您必须向
但我同意AKS,而不是使用
1 | return a - b; |
你应该使用
1 | return a > b ? 1 : a < b ? -1 : 0; |
在javascript中,sort()方法的默认行为是按字母顺序对数组中的值排序。
要按数字排序,您必须定义一个数字排序函数(这非常简单):
1 2 3 4 5 6 7 | ... function sortNumber(a, b) { return a - b; } numArray = numArray.sort(sortNumber); |
array.prototype.sort()是对数组排序的Go-to方法,但我们需要注意一些问题。
排序顺序在默认情况下是词典,而不是数字,不管数组中的值类型如何。即使数组是所有数字,所有值都将转换为字符串并按字典顺序排序。
所以我们需要像下面那样定制sort()和reverse()方法吗?
参考URL
用于对数组中的数字进行排序
1 2 3 4 | numArray.sort(function(a, b) { return a - b; }); |
用于反转数组中的数字
1 2 3 4 | numArray.sort(function(a, b) { return b - a; }); |
参考URL
我很惊讶为什么每个人都建议将比较函数传递给
要对数字排序,只需创建任何类型的数组:
1 2 3 | var numArray = new Uint32Array([140000, 104, 99]); numArray = numArray.sort(); alert(numArray) |
在新的ES6世界里,这样做要容易得多
1 | numArray.sort((a,b) => a-b); |
这就是你所需要的。)
这个问题已经得到了解答,最短的方法是使用
插入排序
提升:
1 2 3 4 5 6 7 8 9 | var numArray = [140000, 104, 99]; for (var i = 0; i < numArray.length; i++) { var target = numArray[i]; for (var j = i - 1; j >= 0 && (numArray[j] > target); j--) { numArray[j+1] = numArray[j]; } numArray[j+1] = target } console.log(numArray); |
下降:
1 2 3 4 5 6 7 8 9 | var numArray = [140000, 104, 99]; for (var i = 0; i < numArray.length; i++) { var target = numArray[i]; for (var j = i - 1; j >= 0 && (numArray[j] < target); j--) { numArray[j+1] = numArray[j]; } numArray[j+1] = target } console.log(numArray); |
选择排序:
提升:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var numArray = [140000, 104, 99]; for (var i = 0; i < numArray.length - 1; i++) { var min = i; for (var j = i + 1; j < numArray.length; j++) { if (numArray[j] < numArray[min]) { min = j; } } if (min != i) { var target = numArray[i]; numArray[i] = numArray[min]; numArray[min] = target; } } console.log(numArray); |
下降:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var numArray = [140000, 104, 99]; for (var i = 0; i < numArray.length - 1; i++) { var min = i; for (var j = i + 1; j < numArray.length; j++) { if (numArray[j] > numArray[min]) { min = j; } } if (min != i) { var target = numArray[i]; numArray[i] = numArray[min]; numArray[min] = target; } } console.log(numArray); |
玩得高兴
下面的"numerically"函数用于在许多情况下作为回调函数对数字数组进行排序:
1 2 3 4 5 | function numerically(a, b){ return a-b; } array.sort(numerically); |
但在一些罕见的情况下,数组包含非常大的负数,由于a-b小于javascript可以处理的最小数字,可能会发生溢出错误。
因此,编写数值函数的更好方法如下:
1 2 3 4 5 6 7 8 9 | function numerically(a, b){ if(a < b){ return -1; } else if(a > b){ return 1; } else { return 0; } } |
按如下方式尝试此代码
1 2 3 4 5 6 7 | var a = [5, 17, 29, 48, 64, 21]; function sortA(arr) { return arr.sort(function(a, b) { return a - b; }) ;} alert(sortA(a)); |
仅限元素值的普通数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function sortArrayOfElements(arrayToSort) { function compareElements(a, b) { if (a < b) return -1; if (a > b) return 1; return 0; } return arrayToSort.sort(compareElements); } e.g. 1: var array1 = [1,2,545,676,64,2,24] **output : [1, 2, 2, 24, 64, 545, 676]** var array2 = ["v","a",545,676,64,2,"24"] **output: ["a","v", 2,"24", 64, 545, 676]** |
对于对象数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function sortArrayOfObjects(arrayToSort, key) { function compareObjects(a, b) { if (a[key] < b[key]) return -1; if (a[key] > b[key]) return 1; return 0; } return arrayToSort.sort(compareObjects); } e.g. 1: var array1= [{"name":"User4","value": 4},{"name":"User3","value": 3},{"name":"User2","value": 2}] **output : [{"name":"User2","value": 2},{"name":"User3","value": 3},{"name":"User4","value": 4}]** |
Update! Scroll to bottom of answer for
smartSort prop additive that gives even more fun!
Sorts arrays of anything!
我个人最喜欢的函数形式允许参数升序或降序:
1 2 3 4 5 6 7 8 9 10 11 12 | function intArraySort(c, a) { function d(a, b) { return b - a; } "string" == typeof a && a.toLowerCase(); switch (a) { default: return c.sort(function(a, b) { return a - b; }); case 1: case"d": case"dc": case"desc": return c.sort(d) } }; |
简单用法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | var ara = function getArray() { var a = Math.floor(Math.random()*50)+1, b = []; for (i=0;i<=a;i++) b.push(Math.floor(Math.random()*50)+1); return b; }(); // Ascending intArraySort(ara); console.log(ara); // Descending intArraySort(ara, 1); console.log(ara); // Ascending intArraySort(ara, 'a'); console.log(ara); // Descending intArraySort(ara, 'dc'); console.log(ara); // Ascending intArraySort(ara, 'asc'); console.log(ara); |
杰西德
或者这里的代码段示例!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | function intArraySort(c, a) { function d(a, b) { return b - a } "string" == typeof a && a.toLowerCase(); switch (a) { default: return c.sort(function(a, b) { return a - b }); case 1: case"d": case"dc": case"desc": return c.sort(d) } }; function tableExample() { var d = function() { var a = Math.floor(50 * Math.random()) + 1, b = []; for (i = 0; i <= a; i++) b.push(Math.floor(50 * Math.random()) + 1); return b }, a = function(a) { var b = $("<tr/>"), c = $("<th/>").prependTo(b); $("<td/>", { text: intArraySort(d(), a).join(",") }).appendTo(b); switch (a) { case 1: case"d": case"dc": case"desc": c.addClass("desc").text("Descending"); break; default: c.addClass("asc").text("Ascending") } return b }; return $("tbody").empty().append(a(), a(1), a(), a(1), a(), a(1), a(), a(1), a(), a(1), a(), a(1)) }; tableExample(); |
1 2 3 4 | table { border-collapse: collapse; } th, td { border: 1px solid; padding: .25em .5em; vertical-align: top; } .asc { color: red; } .desc { color: blue } |
1 2 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> <table><tbody></tbody></table> |
现在,使用一种排序方法来对一个包含多个项目的数组进行排序,会更有趣!目前不包括"关联"(又名字符串键),但它涵盖了所有类型的值!它不仅会对多个值
"为什么?"你问。为什么不!
现在有两种口味!第一种需要更新的浏览器,因为它使用
1 2 3 4 | /* begin */ /* KEY NOTE! Requires EcmaScript 5.1 (not compatible with older browsers) */ ;;(function(){if(Object.defineProperty&&!Array.prototype.smartSort){var h=function(a,b){if(null==a||void 0==a)return 1;if(null==b||void 0==b)return-1;var c=typeof a,e=c+typeof b;if(/^numbernumber$/ig.test(e))return a-b;if(/^stringstring$/ig.test(e))return a>b;if(/(string|number){2}/ig.test(e))return/string/i.test(c)?1:-1;if(/number/ig.test(e)&&/object/ig.test(e)||/string/ig.test(e)&&/object/ig.test(e))return/object/i.test(c)?1:-1;if(/^objectobject$/ig.test(e)){a instanceof Array&&a.smartSort("a");b instanceof Array&&b.smartSort("a");if(a instanceof Date&&b instanceof Date)return a-b;if(a instanceof Array&&b instanceof Array){var e=Object.keys(a),g=Object.keys(b),e=e.concat(g).smartSort("a"),d;for(d in e)if(c=e[d],a[c]!=b[c])return d=[a[c],b[c]].smartSort("a"),a[c]==d[0]?-1:1;var f=[a[Object.keys(a)[0]],b[Object.keys(b)[0]]].smartSort("a");return a[Object.keys(a)[0]]==f[0]?-1:1}if(a instanceof Element&&b instanceof Element){if(a.tagName==b.tagName)return e=[a.id,b.id].smartSort("a"),a.id==e[0]?1:-1;e=[a.tagName, b.tagName].smartSort("a");return a.tagName==e[0]?1:-1}if(a instanceof Date||b instanceof Date)return a instanceof Date?1:-1;if(a instanceof Array||b instanceof Array)return a instanceof Array?-1:1;e=Object.keys(a);g=Object.keys(b);e.concat(g).smartSort("a");for(c=0;20>c;c++){d=e[c];f=g[c];if(a.hasOwnProperty(d)&&b.hasOwnProperty(f)){if(a[d]instanceof Element&&b[f]instanceof Element){if(a[d].tagName==b[f].tagName)return c=[a[d].id,b[f].id].smartSort("a"),a[d].id==c[0]?-1:1;c=[a[d].tagName,b[f].tagName].smartSort("d"); return a[d].tagName==c[0]?1:-1}if(a[d]instanceof Element||b[f]instanceof Element)return a[d]instanceof Element?1:-1;if(a[d]!=b[f])return c=[a[d],b[f]].smartSort("a"),a[d]==c[0]?-1:1}if(a.hasOwnProperty(d)&&a[d]instanceof Element)return 1;if(b.hasOwnProperty(f)&&b[f]instanceof Element||!a.hasOwnProperty(d))return-1;if(!b.hasOwnProperty(d))return 1}c=[a[Object.keys(a)[0]],b[Object.keys(b)[0]]].smartSort("d");return a[Object.keys(a)[0]]==c[0]?-1:1}g=[a,b].sort();return g[0]>g[1]},k=function(a,b){if(null== a||void 0==a)return 1;if(null==b||void 0==b)return-1;var c=typeof a,e=c+typeof b;if(/^numbernumber$/ig.test(e))return b-a;if(/^stringstring$/ig.test(e))return b>a;if(/(string|number){2}/ig.test(e))return/string/i.test(c)?1:-1;if(/number/ig.test(e)&&/object/ig.test(e)||/string/ig.test(e)&&/object/ig.test(e))return/object/i.test(c)?1:-1;if(/^objectobject$/ig.test(e)){a instanceof Array&&a.smartSort("d");b instanceof Array&&b.smartSort("d");if(a instanceof Date&&b instanceof Date)return b-a;if(a instanceof Array&&b instanceof Array){var e=Object.keys(a),g=Object.keys(b),e=e.concat(g).smartSort("a"),d;for(d in e)if(c=e[d],a[c]!=b[c])return d=[a[c],b[c]].smartSort("d"),a[c]==d[0]?-1:1;var f=[a[Object.keys(a)[0]],b[Object.keys(b)[0]]].smartSort("d");return a[Object.keys(a)[0]]==f[0]?-1:1}if(a instanceof Element&&b instanceof Element){if(a.tagName==b.tagName)return e=[a.id,b.id].smartSort("d"),a.id==e[0]?-1:1;e=[a.tagName,b.tagName].smartSort("d");return a.tagName==e[0]?-1:1}if(a instanceof Date||b instanceof Date)return a instanceof Date?1:-1;if(a instanceof Array||b instanceof Array)return a instanceof Array?-1:1;e=Object.keys(a);g=Object.keys(b);e.concat(g).smartSort("a");for(c=0;20>c;c++){d=e[c];f=g[c];if(a.hasOwnProperty(d)&&b.hasOwnProperty(f)){if(a[d]instanceof Element&&b[f]instanceof Element){if(a[d].tagName==b[f].tagName)return c=[a[d].id,b[f].id].smartSort("d"),a[d].id==c[0]?-1:1;c=[a[d].tagName,b[f].tagName].smartSort("d");return a[d].tagName==c[0]?-1:1}if(a[d]instanceof Element||b[f]instanceof Element)return a[d]instanceof Element?1:-1;if(a[d]!=b[f])return c=[a[d],b[f]].smartSort("d"),a[d]==c[0]?-1:1}if(a.hasOwnProperty(d)&&a[d]instanceof Element)return 1;if(b.hasOwnProperty(f)&&b[f]instanceof Element)return-1;if(!a.hasOwnProperty(d))return 1;if(!b.hasOwnProperty(d))return-1}c=[a[Object.keys(a)[0]],b[Object.keys(b)[0]]].smartSort("d");return a[Object.keys(a)[0]]==c[0]?-1:1}g=[a,b].sort();return g[0]<g[1]};Object.defineProperty(Array.prototype,"smartSort",{value:function(){return arguments&& (!arguments.length||1==arguments.length&&/^a([sc]{2})?$|^d([esc]{3})?$/i.test(arguments[0]))?this.sort(!arguments.length||/^a([sc]{2})?$/i.test(arguments[0])?h:k):this.sort()}})}})(); /* end */ |
jsFiddle array.prototype.smartsort('asc_desc')
使用简单!首先做一些疯狂的阵列,比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | window.z = [ 'one', undefined, $('<span />'), 'two', null, 2, $('', { id: 'Thing' }), $(''), 4, $('<header />') ]; z.push(new Date('1/01/2011')); z.push('three'); z.push(undefined); z.push([ 'one', 'three', 'four' ]); z.push([ 'one', 'three', 'five' ]); z.push({ a: 'a', b: 'b' }); z.push({ name: 'bob', value: 'bill' }); z.push(new Date()); z.push({ john: 'jill', jack: 'june' }); z.push([ 'abc', 'def', [ 'abc', 'def', 'cba' ], [ 'cba', 'def', 'bca' ], 'cba' ]); z.push([ 'cba', 'def', 'bca' ]); z.push({ a: 'a', b: 'b', c: 'c' }); z.push({ a: 'a', b: 'b', c: 'd' }); |
然后简单地排序!
1 2 | z.smartSort('asc'); // Ascending z.smartSort('desc'); // Descending |
仅方法
和前面一样,只是一个简单的方法!
1 2 3 4 | /* begin */ /* KEY NOTE! Method `smartSort` is appended to native `window` for global use. If you'd prefer a more local scope, simple change `window.smartSort` to `var smartSort` and place inside your class/method */ window.smartSort=function(){if(arguments){var a,b,c;for(c in arguments)arguments[c]instanceof Array&&(a=arguments[c],void 0==b&&(b="a")),"string"==typeof arguments[c]&&(b=/^a([sc]{2})?$/i.test(arguments[c])?"a":"d");if(a instanceof Array)return a.sort("a"==b?smartSort.asc:smartSort.desc)}return this.sort()};smartSort.asc=function(a,b){if(null==a||void 0==a)return 1;if(null==b||void 0==b)return-1;var c=typeof a,e=c+typeof b;if(/^numbernumber$/ig.test(e))return a-b;if(/^stringstring$/ig.test(e))return a> b;if(/(string|number){2}/ig.test(e))return/string/i.test(c)?1:-1;if(/number/ig.test(e)&&/object/ig.test(e)||/string/ig.test(e)&&/object/ig.test(e))return/object/i.test(c)?1:-1;if(/^objectobject$/ig.test(e)){a instanceof Array&&a.sort(smartSort.asc);b instanceof Array&&b.sort(smartSort.asc);if(a instanceof Date&&b instanceof Date)return a-b;if(a instanceof Array&&b instanceof Array){var e=Object.keys(a),g=Object.keys(b),e=smartSort(e.concat(g),"a"),d;for(d in e)if(c=e[d],a[c]!=b[c])return d=smartSort([a[c], b[c]],"a"),a[c]==d[0]?-1:1;var f=smartSort([a[Object.keys(a)[0]],b[Object.keys(b)[0]]],"a");return a[Object.keys(a)[0]]==f[0]?-1:1}if(a instanceof Element&&b instanceof Element){if(a.tagName==b.tagName)return e=smartSort([a.id,b.id],"a"),a.id==e[0]?1:-1;e=smartSort([a.tagName,b.tagName],"a");return a.tagName==e[0]?1:-1}if(a instanceof Date||b instanceof Date)return a instanceof Date?1:-1;if(a instanceof Array||b instanceof Array)return a instanceof Array?-1:1;e=Object.keys(a);g=Object.keys(b);smartSort(e.concat(g),"a");for(c=0;20>c;c++){d=e[c];f=g[c];if(a.hasOwnProperty(d)&&b.hasOwnProperty(f)){if(a[d]instanceof Element&&b[f]instanceof Element){if(a[d].tagName==b[f].tagName)return c=smartSort([a[d].id,b[f].id],"a"),a[d].id==c[0]?-1:1;c=smartSort([a[d].tagName,b[f].tagName],"a");return a[d].tagName==c[0]?-1:1}if(a[d]instanceof Element||b[f]instanceof Element)return a[d]instanceof Element?1:-1;if(a[d]!=b[f])return c=smartSort([a[d],b[f]],"a"),a[d]==c[0]?-1:1}if(a.hasOwnProperty(d)&&a[d]instanceof Element)return 1; if(b.hasOwnProperty(f)&&b[f]instanceof Element||!a.hasOwnProperty(d))return-1;if(!b.hasOwnProperty(d))return 1}c=smartSort([a[Object.keys(a)[0]],b[Object.keys(b)[0]]],"a");return a[Object.keys(a)[0]]==c[0]?1:-1}g=[a,b].sort();return g[0]>g[1]};smartSort.desc=function(a,b){if(null==a||void 0==a)return 1;if(null==b||void 0==b)return-1;var c=typeof a,e=c+typeof b;if(/^numbernumber$/ig.test(e))return b-a;if(/^stringstring$/ig.test(e))return b>a;if(/(string|number){2}/ig.test(e))return/string/i.test(c)? 1:-1;if(/number/ig.test(e)&&/object/ig.test(e)||/string/ig.test(e)&&/object/ig.test(e))return/object/i.test(c)?1:-1;if(/^objectobject$/ig.test(e)){a instanceof Array&&a.sort(smartSort.desc);b instanceof Array&&b.sort(smartSort.desc);if(a instanceof Date&&b instanceof Date)return b-a;if(a instanceof Array&&b instanceof Array){var e=Object.keys(a),g=Object.keys(b),e=smartSort(e.concat(g),"a"),d;for(d in e)if(c=e[d],a[c]!=b[c])return d=smartSort([a[c],b[c]],"d"),a[c]==d[0]?-1:1;var f=smartSort([a[Object.keys(a)[0]], b[Object.keys(b)[0]]],"d");return a[Object.keys(a)[0]]==f[0]?-1:1}if(a instanceof Element&&b instanceof Element){if(a.tagName==b.tagName)return e=smartSort([a.id,b.id],"d"),a.id==e[0]?-1:1;e=smartSort([a.tagName,b.tagName],"d");return a.tagName==e[0]?-1:1}if(a instanceof Date||b instanceof Date)return a instanceof Date?1:-1;if(a instanceof Array||b instanceof Array)return a instanceof Array?-1:1;e=Object.keys(a);g=Object.keys(b);smartSort(e.concat(g),"a");for(c=0;20>c;c++){d=e[c];f=g[c];if(a.hasOwnProperty(d)&& b.hasOwnProperty(f)){if(a[d]instanceof Element&&b[f]instanceof Element){if(a[d].tagName==b[f].tagName)return c=smartSort([a[d].id,b[f].id],"d"),a[d].id==c[0]?-1:1;c=smartSort([a[d].tagName,b[f].tagName],"d");return a[d].tagName==c[0]?-1:1}if(a[d]instanceof Element||b[f]instanceof Element)return a[d]instanceof Element?1:-1;if(a[d]!=b[f])return c=smartSort([a[d],b[f]],"d"),a[d]==c[0]?-1:1}if(a.hasOwnProperty(d)&&a[d]instanceof Element)return 1;if(b.hasOwnProperty(f)&&b[f]instanceof Element)return-1; if(!a.hasOwnProperty(d))return 1;if(!b.hasOwnProperty(d))return-1}c=smartSort([a[Object.keys(a)[0]],b[Object.keys(b)[0]]],"d");return a[Object.keys(a)[0]]==c[0]?-1:1}g=[a,b].sort();return g[0]<g[1]} /* end */ |
用途:
1 2 | z = smartSort(z, 'asc'); // Ascending z = smartSort(z, 'desc'); // Descending |
jfiddle方法smartsort(数组,"asc desc")
试试这个代码:
HTML:
1 |
javascript代码:
1 2 3 4 5 6 | (function(){ var points = [40, 100, 1, 5, 25, 10]; document.getElementById("demo").innerHTML = points; points.sort(function(a, b){return a-b}); document.getElementById("demo").innerHTML = points; })(); |
这是我在utils库中的sort array函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | sortArray: function(array) { array.sort(function(a, b) { return a > b; }); }, # Let's test a string array var arr = ['bbc', 'chrome', 'aux', 'ext', 'dog']; utils.sortArray(arr); console.log(arr); >>> ["aux","bbc","chrome","dog","ext", remove: function] # Let's test a number array var arr = [55, 22, 1425, 12, 78]; utils.sortArray(arr); console.log(arr); >>> [12, 22, 55, 78, 1425, remove: function] |
要处理undefined、null和nan:null的行为类似于0、nan和undefined,将结束。
1 2 3 | array = [3, 5, -1, 1, NaN, 6, undefined, 2, null] array.sort((a,b) => isNaN(a) || a-b) // [-1, null, 1, 2, 3, 5, 6, NaN, undefined] |