0

I have 2 arrays of objects

array1 = [
    {
        "name": "B",
        "order": 1
    },
    {
        "name": "C",
        "order": 2
    },
    {
        "name": "D",
        "order": 3
    },
    {
        "name": "B",
        "order": 4
    },
    {
        "name": "A",
        "order": 5
    }
]

array2 = [
    {
        "name": "B",
        "order": 1,
        "id": 3638
    },
    {
        "name": "B",
        "order": 1,
        "id": 3661
    },
    {
        "name": "C",
        "order": 2,
        "id": 3658
    },
    {
        "name": "D",
        "order": 3,
        "id": 3659
    },
    {
        "name": "A",
        "order": 5,
        "id": 3636
    }
]

I need to sort array2 to match the same order of array1 based on the property name. In array2 there are 2 names properties equal with value B. These name B are together but i need the array to be in the exact order of array1. In array1 the first B is at index 0 and the second B is at index 3.

I made this attempts without luck.

array2.sort((a, b) => array1.indexOf(a.name) - array1.indexOf(b.name));

let sorted = array2.sort((a, b) => {
        return array1.findIndex(p => p.name=== a.name) - array1.findIndex(p => p.name=== b.name);
      });
André Castro
  • 1,527
  • 6
  • 33
  • 60
  • How to determine which B goes to which order? – holydragon Jul 01 '22 at 09:34
  • You need extra logic to determine which of the two `B` objects has to be first. In array1, the second object named `B`, has a different value for order. But in array2, the second object with the name `B`, has the same order. So in the second array, you need to either change the order, or use the id. So if we add logic that lower id's have to go first, we can make a correct sort. Or come up with a different sorting logic ofcourse. – Shilly Jul 01 '22 at 09:35
  • if there is a way to determine which B' should come first maybe https://stackoverflow.com/questions/14872554/sorting-on-a-custom-order can help – cmgchess Jul 01 '22 at 09:40
  • I need the sort to be based on name index position in array1. Forget the order and id keys they are not elegible for the sort. I will update my code in the post because it as p.order and should be p.name. – André Castro Jul 01 '22 at 09:42
  • In array2 First B will need to be at index 0 and second B will be at index 3 (matching array1 index positions). – André Castro Jul 01 '22 at 09:53

1 Answers1

0

You can try this, it finds the first elem in array2 matching the name, in order, from the array1.. removes it from array2 and then adds it to the sortedArray2.

const array1 = [
    {
        "name": "B",
        "order": 1
    },
    {
        "name": "C",
        "order": 2
    },
    {
        "name": "D",
        "order": 3
    },
    {
        "name": "B",
        "order": 4
    },
    {
        "name": "A",
        "order": 5
    }
]

const array2 = [
    {
        "name": "B",
        "order": 1,
        "id": 3638
    },
    {
        "name": "B",
        "order": 1,
        "id": 3661
    },
    {
        "name": "C",
        "order": 2,
        "id": 3658
    },
    {
        "name": "D",
        "order": 3,
        "id": 3659
    },
    {
        "name": "A",
        "order": 5,
        "id": 3636
    }
]

const sortedArray2 = []

for(const item of array1) {
    sortedArray2.push(
        array2.splice(
            array2.findIndex(elem => elem.name === item.name), 1
        )
    )
}

console.log(sortedArray2)
Fahd Lihidheb
  • 671
  • 4
  • 20