考虑以下内容:
我在HTML表中的数组中表示数据,例如:
1)我如何按b1
或b3
对数组进行排序?
我试过了:
var o = {
"orgs": {
"655": {
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"depth": 1,
"agents": [207072],
"orgunit_id": "TEAM00655",
"name": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"853": {
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"depth": 2,
"agents": [208162],
"orgunit_id": "TEAM00853",
"name": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"921": {
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"depth": 1,
"agents": [210171, 212842],
"orgunit_id": "TEAM00921",
"name": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"agents": {
"207072": {
"name": "Bongiwe Gwala",
"oid": 655,
"depth": 1,
"aid": "A0207072",
"orgunit_id": "TEAM00655",
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"aname": "A0207072: Bongiwe Gwala",
"oname": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"208162": {
"name": "Nkosikhona MADLALA",
"oid": 853,
"depth": 2,
"aid": "A0208162",
"orgunit_id": "TEAM00853",
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"aname": "A0208162: Nkosikhona MADLALA",
"oname": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"212842": {
"name": "SANELE KHUMALO",
"oid": 921,
"depth": 1,
"aid": "A0212842",
"orgunit_id": "TEAM00921",
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"aname": "A0212842: SANELE KHUMALO",
"oname": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"orglist": [853, 655, 921],
}
function sort_data(data, sortby, asc) {
console.log(data);
if (asc == "asc") {
data.sort(function(a, b) {
a.sortby - b.sortby;
});
} else {
data.sort(function(a, b) {
a.sortby + b.sortby;
});
}
// update_data;
}
var a = sort_data(o, "b1", "asc");
console.log(a);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
更新
感谢@NicolasAlbert,我得到了分类工作。
现在我需要按agents
订购。它需要首先按orgs
排序,然后按agents
排序我尝试过:
if (asc == "desc") {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
// return d - c;
return b_org - a_org && a_agent - b_agent;
// data.agents[a].data[sortby] - data.agents[b].data[sortby]
});
} else {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return a_org - b_org && a_agent - b_agent;
});
}
这无论如何都不起作用......
另一次更新
我修改了我的代码:
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
agents = data.orgs[b].agents.sort(function(a, b){
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return b_agent - a_agent
});
return b_org - a_org && agents;
});
但是,这会同时对orgs
和agents
进行排序。
最新消息:
我让它工作,对orgs
和agents
进行排序,我必须创建两个排序函数:
function sort_org(data, sortby, order) {
/*
Sort the orgs
*/
var a_org, b_org;
if (order == "desc") {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
return b_org - a_org;
});
} else {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
return a_org - b_org;
});
}
}
function sort_agent(data, sortby, order) {
/*
Sort the agents
*/
var a_agent, b_agent;
if (order == "desc") {
for (var orgid in data.orglist){
data.orgs[data.orglist[orgid]].agents.sort(function(a, b){
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return b_agent - a_agent
})
}
} else {
for (var orgid in data.orglist){
data.orgs[data.orglist[orgid]].agents.sort(function(a, b){
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return a_agent - b_agent
})
}
}
}
然后我只是连续调用函数......即
sort_org(o, "b1", "asc");
sort_agent(o, "b1", "asc");
我希望这可以帮助别人......
答案 0 :(得分:1)
您只能在.sort
个实例上使用Array
方法,而不能Object
。您的数据存储在键/值对象中,并且无法修改订单。
如果您想订购数据,则必须在其中引入Array
([]
)。
您是否希望订购orglist
数组:
var o = {
"orgs": {
"655": {
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"depth": 1,
"agents": [207072],
"orgunit_id": "TEAM00655",
"name": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"853": {
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"depth": 2,
"agents": [208162],
"orgunit_id": "TEAM00853",
"name": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"921": {
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"depth": 1,
"agents": [210171, 212842],
"orgunit_id": "TEAM00921",
"name": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"agents": {
"207072": {
"name": "Bongiwe Gwala",
"oid": 655,
"depth": 1,
"aid": "A0207072",
"orgunit_id": "TEAM00655",
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"aname": "A0207072: Bongiwe Gwala",
"oname": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"208162": {
"name": "Nkosikhona MADLALA",
"oid": 853,
"depth": 2,
"aid": "A0208162",
"orgunit_id": "TEAM00853",
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"aname": "A0208162: Nkosikhona MADLALA",
"oname": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"212842": {
"name": "SANELE KHUMALO",
"oid": 921,
"depth": 1,
"aid": "A0212842",
"orgunit_id": "TEAM00921",
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"aname": "A0212842: SANELE KHUMALO",
"oname": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"orglist": [853, 655, 921]
}
function sort_data(data, sortby, asc) {
console.log(data);
if (asc == "asc") {
data.orglist.sort(function(a, b) {
data.orgs[a].data[sortby] - data.orgs[b].data[sortby];
});
} else {
data.orglist.sort(function(a, b) {
data.orgs[b].data[sortby] - data.orgs[a].data[sortby];
});
}
// update_data;
}
sort_data(o, "b1", "asc");
console.log(o.orglist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>