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;
	}
}

posted @ 2019-01-02 12:08  栖息地  阅读(218)  评论(0编辑  收藏  举报