Skip to content

Instantly share code, notes, and snippets.

@hkongm
Created September 18, 2013 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkongm/6602780 to your computer and use it in GitHub Desktop.
Save hkongm/6602780 to your computer and use it in GitHub Desktop.
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