简体   繁体   English

基于其他数组中的索引对对象数组进行排序

[英]Sorting Array of Objects based on Index in other array

I have the following array of unique IDs: 我有以下数组的唯一ID:

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. 优选使用lodash,因为阵列可以生长得稍大。

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: 尝试_.map()和_.sortBy()的不同组合我似乎无法正确获得我想要的结果:

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

This is possible to do without any library using Array.sort() 没有任何库使用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) }); 

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). 排序是“最好”的O(2n)操作,而每个循环有两个indexOf操作,另一个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) 只有O(n ^ 2)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM