我想基于两个字段对json数据进行排序。我想首先基于geo和用户名对数据进行排序我想基于geo进行排序并在geo内对用户进行排序。例如在geo'Europe'中 按字母顺序对用户进行排序,然后按“中东”地理位置
const response = [{Username: "Denisse Morales", Geo: "NSU", Status: "Reaches", month: "May", Week2: 2},
{Username: "Chandra Jeevan J", Geo: "US East", Status: "Reaches", month: "May", Week2: 1},
{Username: "Amy Khalil", Geo: "US West", Status: "Reaches", month: "May", Week2: 2},
{Username: "Ashuwinth Panneer", Geo: "Europe", Status: "Reaches", month: "June", Week2: 1},
{Username: "John Joshuva", Geo: "Europe", Status: "Reaches", month: "May", Week2: 3},
{Username: "Jose Alberto", Geo: "Europe", Status: "Reaches", month: "April", Week2: 2},
{Username: "Dhivya Muthusamy", Geo: "Europe", Status: "Reaches", month: "June", Week2: 10},
{Username: "Faizan Mohammed", Geo: "Middle East", Status: "Reaches", month: "May", Week2: 3},
{Username: "Jaison Clinton", Geo: "NSU", Status: "Reaches", month: "April", Week2: 9}];
response.sort((a, b) => a.Geo.localeCompare(b.Geo));
response.sort((a, b) => a.Username.localeCompare(b.Username));
console.log(response);
答案 0 :(得分:0)
我想你想要这样的东西
const response = [ { Username: "Denisse Morales", Geo: "NSU", Status: "Reaches", month: "May", Week2: 2, }, { Username: "Chandra Jeevan J", Geo: "US East", Status: "Reaches", month: "May", Week2: 1, }, { Username: "Amy Khalil", Geo: "US West", Status: "Reaches", month: "May", Week2: 2, }, { Username: "Ashuwinth Panneer", Geo: "Europe", Status: "Reaches", month: "June", Week2: 1, }, { Username: "John Joshuva", Geo: "Europe", Status: "Reaches", month: "May", Week2: 3, }, { Username: "Jose Alberto", Geo: "Europe", Status: "Reaches", month: "April", Week2: 2, }, { Username: "Dhivya Muthusamy", Geo: "Europe", Status: "Reaches", month: "June", Week2: 10, }, { Username: "Faizan Mohammed", Geo: "Middle East", Status: "Reaches", month: "May", Week2: 3, }, { Username: "Jaison Clinton", Geo: "NSU", Status: "Reaches", month: "April", Week2: 9, }, ];
response.sort(
(a, b) => a.Geo.localeCompare(b.Geo) || a.Username.localeCompare(b.Username)
);
console.log(response);
答案 1 :(得分:0)
您可以比较串联的地理位置和用户名。
const response = [{Username: "Denisse Morales", Geo: "NSU", Status: "Reaches", month: "May", Week2: 2},
{Username: "Chandra Jeevan J", Geo: "US East", Status: "Reaches", month: "May", Week2: 1},
{Username: "Amy Khalil", Geo: "US West", Status: "Reaches", month: "May", Week2: 2},
{Username: "Ashuwinth Panneer", Geo: "Europe", Status: "Reaches", month: "June", Week2: 1},
{Username: "John Joshuva", Geo: "Europe", Status: "Reaches", month: "May", Week2: 3},
{Username: "Jose Alberto", Geo: "Europe", Status: "Reaches", month: "April", Week2: 2},
{Username: "Dhivya Muthusamy", Geo: "Europe", Status: "Reaches", month: "June", Week2: 10},
{Username: "Faizan Mohammed", Geo: "Middle East", Status: "Reaches", month: "May", Week2: 3},
{Username: "Jaison Clinton", Geo: "NSU", Status: "Reaches", month: "April", Week2: 9}];
response.sort((a, b) => {
return (a.Geo + a.Username) > (b.Geo + b.Username) ? 1 : -1;
});
console.log(response);