Home > other >  Sort 2 objects with the same key Javascript
Sort 2 objects with the same key Javascript

Time:12-04

I have 2 objects where I want the second one to be in the same order as the first one.

Ex:

const obj1 = [
  { key: 1, id: 1, name: "John" },
  { key: 2, id: 2, name: "Ann" },
  { key: 3, id: 3, name: "Kate" }
];

const obj2 = [
  { key: 2, id: 2, name: "Ann" },
  { key: 1, id: 1, name: "John" },
  { key: 3, id: 3, name: "Kate" }
];

The purpose is to have obj2 in same order as obj1, but only sort with keys. I'm trying to make an helper function which will pass 3 argument:

function helper(obj1, obj2, key) {
  // return new object with sorted array do not modify existing
}

I can sort one of this, but cant combine 2object together

CodePudding user response:

To sort two objects with the same key in JavaScript, you can use the Array.sort method.

The Array.sort method allows you to specify a comparator function that determines the sorting order for each element in the array. The comparator function should take two arguments, and it should return a negative value if the first argument should be sorted before the second argument, a positive value if the first argument should be sorted after the second argument, and 0 if the two arguments are equal.

Here is an example of how to use the Array.sort method to sort two objects with the same key in JavaScript:

// Define two objects with the same key
const obj1 = {key: 1, value: "apple"};
const obj2 = {key: 1, value: "orange"};

// Define the comparator function
function comparator(a, b) {
  // Compare the key values of the objects
  return a.key - b.key;
}

// Use the sort method to sort the objects
const sortedObjects = [obj1, obj2].sort(comparator);

// Print the sorted objects
console.log(sortedObjects);

In this example, the comparator function is defined to compare the key values of the objects and return the difference as the sorting order. The Array.sort method is then used to sort the objects based on this comparator function.

As you can see, the objects are sorted based on the key value, and the objects with the same key are preserved in the original order.

You can modify the comparator function to use different values as the sorting key. For example, you can use the value field of the objects as the sorting key instead of the key field. Here is how to do this:

CodePudding user response:

My idea is to use hash map to store key obj2, then loop through key obj1 to sort obj2

 const obj1 = [
    {key: 1, id: 5, name: 'John'},
    {key: 2, id: 5, name: 'John'},
    {key: 3, id: 5, name: 'John'}
 ]

 const obj2 = [
    {key: 2, id: 5, name: 'John'},
    {key: 1, id: 5, name: 'John'},
    {key: 3, id: 5, name: 'John'}
]

function help(obj1, obj2, key) {
  const hashKeyObj2 = obj2.reduce((val, item) => {
    val[item[key]] = item;
    return val;
  }, {});
  return obj1.map((item) => hashKeyObj2[item[key]]);
}

console.log(help(obj1, obj2, 'key'))

CodePudding user response:

One-liner

let sorted = obj1.map(a => obj2.find(b => a.key === b.key))

CodePudding user response:

This snippet will sort obj2 from obj1 order:

const obj1 = [
  { key: 1, id: 5, name: 'John' },
  { key: 3, id: 5, name: 'John' },
  { key: 2, id: 5, name: 'John' },
]

const obj2 = [
  { key: 2, id: 5, name: 'John' },
  { key: 1, id: 5, name: 'Jane' },
  { key: 3, id: 5, name: 'Tom' },
]

function helper(obj1, obj2, key) {
  const findIndex = (refValue) => obj1.findIndex((candidate) => candidate[key] === refValue)
  const comparator = (item1, item2) => findIndex(item1[key]) - findIndex(item2[key])
  return [...obj2].sort(comparator)
}

const result = helper(obj1, obj2, 'key')

console.log('result :', result)

  • Related