类数组对象排序

var arr = [];
	arr["a"]=123;
	arr["f"] = 34;
	arr["e"] = 234;

和普通数组不一样的是该类型数组使用Object.prototype.toString.call正常输出”[object Array]”但无长度,使用length获取为0,针对这种类型的数组,需要转换成普通数组对象字面量的格式,如下示例:

var pic = [], arr = [], i = 0;
	pic["a"]=123;
	pic["f"] = 34;
	pic["e"] = 234;
	console.log(pic.length);
	var pickeys = Object.keys(pic);
	for(var key in pickeys){
		arr[i] = {
			"name": pickeys[key],
			"value": pic[pickeys[key]]
		}
		i++;
	}

	function sortPIc (name, order) {
		order = (order == "asc") ? 1 : -1
		return function (a, b) {
			return a[name] > b[name] ? order*1 : order*-1;
		}
	}
	console.log(arr.sort(sortPIc("name","asc")));

发表评论


昵称

沙发空缺中,还不快抢~