user1202888
user1202888

Reputation: 1043

Sorting Array of Objects based on Index in other array

I have the following array of unique IDs:

idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"]

And I have another array of objects:

stories = [{"title": Story2, id = "56f4cf96dd2ca7275feaf7b7"},
{"title": Story4, id = "56f4cf96dd2ca7275feaf7ac"},
{"title": Story1, id = "56f4cf96dd2ca7275feaf802"},
{"title": Story3, id = "56f4cf96dd2ca7275feaf805"}]

How can I sort the second array based on the index of the first array? Preferably using lodash, as the arrays can grow a bit larger.

So far, I have the following to get the indexes from the first array:

var sortArray = _.toPairs(idArray)

[ [ '0', 56f4cf96dd2ca7275feaf802 ],
[ '1', 56f4cf96dd2ca7275feaf7b7 ],
[ '2', 56f4cf96dd2ca7275feaf805 ],
[ '3', 56f4cf96dd2ca7275feaf7ac ] ]

Trying different combinations of _.map() and _.sortBy() I can't seem to properly get the result I want which is:

desiredResult = [{"title": Story1, id = "56f4cf96dd2ca7275feaf802"},
          {"title": Story2, id = "56f4cf96dd2ca7275feaf7b7"},
          {"title": Story3, id = "56f4cf96dd2ca7275feaf805"},
          {"title": Story4, id = "56f4cf96dd2ca7275feaf7ac"}]

Upvotes: 3

Views: 3852

Answers (3)

Redu
Redu

Reputation: 26161

I believe the sort solution is very ineffective especially since you expect the arrays to grow bigger later. Sort is "at best" an O(2n) operation while you have two indexOf operations per cycle another O(2n^2). I propose the following which will outperform the sort method in large arrays.

var stories = [{"title": 'Story2', id : "56f4cf96dd2ca7275feaf7b7"},
{"title": 'Story4', id : "56f4cf96dd2ca7275feaf7ac"},
{"title": 'Story1', id : "56f4cf96dd2ca7275feaf802"},
{"title": 'Story3', id : "56f4cf96dd2ca7275feaf805"}],

    idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"],

ordered = idArray.reduce((p,c) => p.concat(stories.find(f => f.id == c)) ,[]);

console.log(ordered);

Only O(n^2)

Upvotes: 4

Jose Hermosilla Rodrigo
Jose Hermosilla Rodrigo

Reputation: 3683

This is possible to do without any library using Array.sort()

var stories = [{"title": 'Story2', id : "56f4cf96dd2ca7275feaf7b7"},
{"title": 'Story4', id : "56f4cf96dd2ca7275feaf7ac"},
{"title": 'Story1', id : "56f4cf96dd2ca7275feaf802"},
{"title": 'Story3', id : "56f4cf96dd2ca7275feaf805"}];

var idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"];

var ordered = stories.sort(function(a, b){
	return idArray.indexOf(a.id) - idArray.indexOf(b.id);
});

ordered.forEach( element =>{ console.log(element) });

Upvotes: 3

Alastair Brown
Alastair Brown

Reputation: 1616

Try this

var idsToIndexes = {};

for (var i = 0; i < idArray.length; i++)
    idsToIndexes[idArray[i]] = i;

stories.sort(function(a, b){return idsToIndexes[a.id] - idsToIndexes[b.id];});

Upvotes: 1

Related Questions