我试图通过id对对象数组进行排序。
对象中有很多数据,但我只粘贴相关的数据。
它工作在小数组中,但不是很大。
http://codepen.io/anon/pen/EZOoKG?editors=1010
这就是我的尝试:
dic = dic.sort(function(a,b){
let aId = a[0].id;
let bId = b[0].id;
return bId > aId;
});
由于
答案 0 :(得分:1)
Javascript Array#sort
就地工作。你可以按字符串排序,因为你有字符串。
var dic = [[{ id: '3e19f60c-8659-4637-9262-6f6f76b17528'}], [{ id: '45b0d86f-eda6-4edf-91ba-7307feca8301'}], [{ id: '324d700d-9f83-46e6-8069-6aca5fff49e9'}], [{ id: '493eb7ec-6b87-475c-9ef8-51f056659576'}], [{ id: '4d9afb9b-efbe-4b57-a544-dc9a830d667f'}], [{ id: '4636937e-acd6-499b-90f4-7a62070c5a21'}], [{ id: '36b6132e-859c-473a-9138-d6311d71e7d4'}], [{ id: '4acb2c25-b5fc-49e2-a53b-5540b0c21ca3'}], [{ id: '36160507-1e0e-46fc-8b72-ae64b1f4c847'}], [{ id: '390cc3dc-e9a3-4c59-896f-a549a7b69a2c'}], [{ id: '438dad38-064c-4d46-88fd-8c2706072f68'}], [{ id: '37ebeed7-e094-464f-b6e5-2bb682f556d1'}], [{ id: '356c5580-c005-49d5-aa05-d38765b17ae9'}], [{ id: '3fab48f9-9361-4bd3-adc9-2a84482aa056'}], [{ id: '4187176e-0d2f-456a-bca3-cd7174947c73'}], [{ id: '334f787c-7432-40f1-8298-9770ce718c30'}], [{ id: '3f49ffb3-706e-4fb9-8781-1ac4fa0740d8'}], [{ id: '461da0e2-32e1-4bdc-8e14-5975a24727a0'}], [{ id: '373d525f-fa4f-4bbe-8660-fe87c85d88b4'}], [{ id: '445503b0-7a2c-486d-99ee-41e57689f6d7'}], [{ id: '46488a62-9fc0-4328-bc3f-54c794009890'}], [{ id: '4b683340-fd3c-4acb-aa9e-60402d66a4ff'}], [{ id: '485342b3-b45f-4e35-b8f4-130f10e5d6ef'}], [{ id: '416d8bfe-924a-4554-bde5-010601bce668'}], [{ id: '3883ae4d-d7e1-4215-85a8-56c500c8c657'}], [{ id: '49fdebfa-afb3-45bb-9d75-2e5e2f296acd'}], [{ id: '4003a0cc-ccb4-457f-8939-af7b5c0fc39d'}]];
dic.sort(function(a, b){
return a[0].id.localeCompare(b[0].id);
});
console.log(dic);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
这似乎有效:
const sortedDic = _.sortBy(_.flatten(dic, function(x) {return x.id;}), ['id']);
sortedDic.forEach(function(i) {console.log(i.id)});