最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sort object array based on another array of keys - Stack Overflow

programmeradmin5浏览0评论

I have two arrays containing objects. One contains keys in some order and another has data and I need to sort the data array in order against the given sorted key array. How can I do this?

var a = ['d','a','b','c'] ;
var b =  [{a:1},{c:3},{d:4},{b:2}];

The result should be:

result = [{d:4},{a:1},{b:2},{c:3]

I have two arrays containing objects. One contains keys in some order and another has data and I need to sort the data array in order against the given sorted key array. How can I do this?

var a = ['d','a','b','c'] ;
var b =  [{a:1},{c:3},{d:4},{b:2}];

The result should be:

result = [{d:4},{a:1},{b:2},{c:3]
Share Improve this question edited Apr 14, 2016 at 9:53 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Apr 14, 2016 at 9:52 A.T.A.T. 26.3k8 gold badges49 silver badges67 bronze badges 2
  • Just FYI; nothing about this has to do with JSON - I have edited as such. – Rory McCrossan Commented Apr 14, 2016 at 9:54
  • Possible duplicate of Javascript - sort array based on another array – Heretic Monkey Commented Apr 1, 2018 at 2:46
Add a ment  | 

5 Answers 5

Reset to default 10

Try this

var a = ['d','a','b','c'] ;
var b =  [{a:1},{c:3},{d:4},{b:2}];

b.sort(function(x,y){
  var xkey = a.indexOf(Object.keys(x)[0]);
  var ykey = a.indexOf(Object.keys(y)[0]);
  return xkey - ykey;
})

document.body.innerHTML += JSON.stringify(b,0,4);

A different approach from above ones would be, using Lodash Javascript Library.

var a = ['d','a','b','c'] ;
var b =  [{a:1},{c:3},{d:4},{b:2}];

var results = _.map(a,function(av) {
   var obj = {};
   obj[av] = _.find(b, av)[av];
   return obj
});

document.body.innerHTML += JSON.stringify(results);
<script src="https://cdn.jsdelivr/lodash/4.11.1/lodash.min.js"></script>

This approach respects the keys in the objects of the array for sorting.

Only the items of a are used for lookup and their respective order.

In this case d gets all sort value of the item of b, so d looks like

[ 1, 3, 0, 2 ]

While sorting with indices, we need e, which has simply the indices of b

[ 0, 1, 2, 3 ]

after sorting it became

[ 2, 0, 3, 1 ]

the wanted sort order. Now the original array is remapped to the new order.

But why?

Usually objects contains more than one property. If you use Object.keys and take just the first element, then you could go wrong, because this element is not the wanted element for getting the sort order.

To make it save, you need a different approach, which does not use Array#indexOf in bination with a fixed item of Object.keys.

var a = ['d', 'a', 'b', 'c'],
    b = [{ a: 1 }, { c: 3 }, { d: 4 }, { b: 2 }],
    d = b.map(function (bb) {
        var k = -1;
        a.some(function (aa, i) {
            if (aa in bb) {
                k = i;
                return true;
            }
        });
        return k;
    }),
    e = b.map(function (_, i) { return i; });

e.sort(function (a, b) {
    return d[a] - d[b];
});

b = e.map(function (a) {
    return b[a];
});

document.write('<pre> ' + JSON.stringify(b, 0, 4) + '</pre>');

This should do the trick

result = a.map(function(key) {
    for(var i=0; i<b.length; ++i) {
        if(key in b[i]) return b[i];
    }
});

Brute force approach is to loop through each of a array and check the b array for it's presence.

var a = ['d','a','b','c'] ;
var b =  [{a:1},{c:3},{d:4},{b:2}];

var ans = [];

for(var i = 0; i < a.length; ++i)
{
 for(var j = 0; j < b.length; ++j)
 {
   if(b[j][a[i]])
     ans.push(b[j]);
 }
}
document.write(JSON.stringify(ans, 0, 4));

发布评论

评论列表(0)

  1. 暂无评论