各位大侠,我从数据库中读取一组数据,排序如下
ID Name Sort
1 北京 1
2 上海 3
3 江苏 4
4 安徽 2我想对这组数据在客户端重新排列
ID Name Sort
1 北京 1
4 安徽 2
2 上海 3
3 江苏 4
希望可以通过jquery的某种方式进行排列,请问该如果处理?不考虑从数据库直接读出根据排序的数据
在线等 谢谢
ID Name Sort
1 北京 1
2 上海 3
3 江苏 4
4 安徽 2我想对这组数据在客户端重新排列
ID Name Sort
1 北京 1
4 安徽 2
2 上海 3
3 江苏 4
希望可以通过jquery的某种方式进行排列,请问该如果处理?不考虑从数据库直接读出根据排序的数据
在线等 谢谢
var json = {"city": [
{"id": "1", "name": "北京 ", "sort": "1"},
{"id": "2", "name": "上海", "sort": "3"},
{"id": "3", "name": "江苏", "sort": "4"},
{"id": "4", "name": "安徽", "sort": "2"}]
};
// $.each(json.city, function(i) {
// alert(json.city[i].id+" " +json.city[i].name+" sort:"+json.city[i].sort);
// });
json["city"].sort(function(a,b){
return a["sort"]>b["sort"]?1:a["sort"]==b["sort"]?0:-1
});
$.each(json.city, function(i) {
alert(json.city[i].id+" " +json.city[i].name+" sort:"+json.city[i].sort);
});
+1js 数组 有sort 函数
{"id": "1", "name": "北京 ", "sort": "1"},
{"id": "2", "name": "上海", "sort": "3"},
{"id": "3", "name": "江苏", "sort": "4"},
{"id": "4", "name": "安徽", "sort": "2"}]
}; json.city.sort(function(a,b){
return a.sort-b.sort;
});alert(json.city[1].name); // 这句是测试用的 结果是 "安徽"--数组中的第二个元素,说明json已经排序好了