Separate negative and positive numbers in array with javascript
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to separate the negative & positive elements of an array in Javascript, such that afterwards first come all negative elements and then the positive elements, each in the original order. Example: Input array: [1,2,-3,-2,4] Output array: [-3,-2,1,2,4] Input array: [3,2,-1,0,-4,3,6,-7,-6] Output array: [-1,-4,-7,-6,3,2,0,3,6] I can do it using a temporary array with use of push() method, but how to do this without using a temporary array in that array only? 回答1: Use sort() var res = [1, 2, -3, -2, 4].sort(function(a, b) { return a -