js对象排序
var map = { "13": "13米", "14": "14米", "15": "15米", "17": "17米", "18": "18米", "6.8": "6.8米", "8.6": "8.6米", "9.6": "9.6米", "4.2": "4.2米", "5.2": "5.2米", "17.5": "17.5米", "6.2": "6.2米", "7.2": "7.2米", "4.5": "4.5米", "8.2": "8.2米", "11.7": "11.7米", "13.5": "13.5米", "7.6": "7.6米", "12.5": "12.5米" } var keyArray = quickSort(getKeyArray(map)); function quickSort(arr){ //如果数组<=1,则直接返回 if(!arr || arr.length<=1){return arr;} var pivotIndex=Math.floor(arr.length/2); //找基准,并把基准从原数组删除 var pivot=arr.splice(pivotIndex,1)[0]; //定义左右数组 var left=[]; var right=[]; //比基准小的放在left,比基准大的放在right for(var i=0;i<arr.length;i++){ if(arr[i]<=pivot){ left.push(arr[i]); } else{ right.push(arr[i]); } } //递归 return quickSort(left).concat([pivot],quickSort(right)); } function getKeyArray(obj){ if(obj && (Object.prototype.toString.call(obj)==='[object Object]')){ var arr = []; for(var i in obj){ arr.push(parseFloat(i)); } return arr; }else{ return null; } }
相关推荐
-
指定html转换成pdf javascript
2019-1-7
-
canvas旋转图片 javascript
2019-1-7
-
点击空白处关闭弹窗 [ JavaScript ] javascript
2019-1-7
-
js 模拟php ksort排序 javascript
2019-1-8
-
JavaScript校验网址 javascript
2019-1-7
-
webpack相关配置 javascript
2019-1-7
-
js、jQuery各种高度 javascript
2019-1-8
-
js正则验证 javascript
2019-1-8
-
微信自定义分享 javascript
2019-1-8
-
JS等比例缩放图片,限定最大宽度和最大高度 javascript
2019-1-7