0

I have 2 arrays:

var one = ['da22', 'ye66', '17hy'] and

var two = [{publicID: 'da22', score: '2'}, {publicID: '17hy', score: '2'}, {publicID: 'ye66', score: '2'}]

I want two to be ordered by publicID according to one so it should be

two = [{publicID: 'da22', score: '2'}, {publicID: 'ye66', score: '2'}, {publicID: '17hy', score: '2'}]

Is there a built in method in NodeJS to do this?

I've got it working however its not very efficient. I have a method than can move an element from one index to another moveFromTo(oldIndex, newIndex which I'm using with:

for (var r=0; r<one.length; r++) {
              if (one[r] != two[r]['publicID']) {
                two.moveFromTo(one.indexOf(two[r]['publicID']), r)
                r=-1; continue;
              }
}

But having to use that r=-1 as things move - although works - doesn't seem like the best of ideas.

Any ideas would be appreciated.

Many Thanks.

4
  • Define what you desire to have happen for objects in two for which there is no matching publicID in one. Commented Sep 1, 2016 at 20:51
  • @Makyen - there will always be a matching object Commented Sep 1, 2016 at 21:00
  • 1
    @Makyen - typo fixed Commented Sep 1, 2016 at 21:00
  • So there will always be an exact one-to-one correspondence between one and two? Commented Sep 1, 2016 at 21:08

2 Answers 2

1

You can do this with sort() and indexOf()

var one = ['da22', 'ye66', '17hy'];
var two = [{publicID: 'da22', score: '2'}, {publicID: '17hy', score: '2'}, {publicID: 'ye66', score: '2'}];

var result = two.sort(function(a, b) {
  return one.indexOf(a.publicID) - one.indexOf(b.publicID);
})

console.log(result)

You can also create object from one and sort by that object.

var one = ['da22', 'ye66', '17hy'];
var two = [{publicID: 'da22', score: '2'}, {publicID: '17hy', score: '2'}, {publicID: 'ye66', score: '2'}];

var o = one.reduce((r, e, i) => {return r[e]=i, r}, {});

var result = two.sort(function(a, b) {
  return o[a.publicID] - o[b.publicID];
})

console.log(result)

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

7 Comments

Assigning to result is not needed. .sort() sorts the array in place. In addition, it returns the array. However, assigning it to result is equivalent to two.sort(function ...); var result=two;.
Side note.. If you're looking for speed, Array.sort() is not your best option by a considerable margin. See stackoverflow.com/questions/38732480/…
@PatrickMotard - The maximum number of items in the array would be 100 - I'm assuming that would be fine?
If the set is that small then there's likely no reason to over complicate for the sake of performance. Best to choose whatever is most readable no?
|
0

I believe there is a simpler solution. You need to synchronize the order of elements in two arrays by different properties of elements of these arrays, while the first (target) array preserves its order.

To do that, you can reduce the two, and for every current element the reduce function is applied to, put corresponding element of two at certain position in resulting array (aka total):

const one = ['da22', 'ye66', '17hy'];
const two = [{
  publicID: 'da22',
  score: '2'
}, {
  publicID: 'ye66',
  score: '2'
}, {
  publicID: '17hy',
  score: '2'
}];

const sortedTwo = two.reduce((total, current) => {
  total[one.indexOf(current.publicID)] = current; // <- some crazy stuff here

  return total;
}, []);

Make sure publicID property values in two and the values of one are perfectly identical sets.

Speed and immutability of the original array included.

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.