1

I am trying to sort an array of arrays of objects by id.

There are lots of data in the object, but I pasted only the relevant.

It works in small array, but not in big.

http://codepen.io/anon/pen/EZOoKG?editors=1010

This is what I tried:

dic = dic.sort(function(a,b){
  let aId = a[0].id;
  let bId = b[0].id;
  return bId > aId;
});

Thanks

3

2 Answers 2

2

Javascript Array#sort works in situ. You could sort by string, because you have strings.

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; }

Sign up to request clarification or add additional context in comments.

Comments

1

This seems to work:

const sortedDic = _.sortBy(_.flatten(dic, function(x) {return x.id;}), ['id']); sortedDic.forEach(function(i) {console.log(i.id)});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.