Created
September 18, 2013 00:30
-
-
Save hkongm/6602780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var willSort = [ | |
{ | |
name:'shangwenhe', | |
age:25, | |
height:170 | |
}, | |
{ | |
name:'zhangsan', | |
age:31, | |
height:169 | |
}, | |
{ | |
name:'lisi', | |
age:31, | |
height:167 | |
}, | |
{ | |
name:'zhaowu', | |
age:22, | |
height:160 | |
}, | |
{ | |
name:'wangliu', | |
age:23, | |
height:159 | |
} | |
]; | |
|* | |
@function JsonSort 对json排序 | |
@param json 用来排序的json | |
@param key 排序的键值 | |
*| | |
function JsonSort(json,key){ | |
console.log(json); | |
for(var j=1,jl=json.length;j < jl;j++){ | |
var temp = json[j], | |
val = temp[key], | |
i = j-1; | |
while(i >=0 && json[i][key]>val){ | |
json[i+1] = json[i]; | |
i = i-1; | |
} | |
json[i+1] = temp; | |
} | |
console.log(json); | |
return json; | |
JsonSort(willSort,'height'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment