Sort objArray2 according to strArray1

the problems encountered in the project require an algorithm that satisfies the function, and the time complexity is as small as possible
two arrays strArray1 and objArray2 :

are known.
const strArray1 = [
    "a",
    "b",
    "c",
    ...
    "z"
]

const objArray2 = [
    {
        str: "c",
        ...
    },
    {
        str: "a",
        ...
    },
    {
        str: "z",
        ...
    },
    ...
    {
        str: "b",
        ...
    }
]

the length of the two arrays is the same. The elements in objArray2 correspond to strArray1 elements one by one, but disordered
now requires objArray2 to be sorted according to the order of strArray1 :

return = [
    {
        str: "a",
        ...
    },
    {
        str: "b",
        ...
    },
    {
        str: "c",
        ...
    },
    ...
    {
        str: "z",
        ...
    }
]
Oct.28,2021

function sortArr(strArray1, objArray2) {
  let rst = [...objArray2]

  // strArray1:{a: 0, b: 1, c: 2, d: 3, ...}
  let obj = strArray1.reduce((iter, val, idx) => {
    iter[val] = idx 
    return iter
  }, {})

  // sort
  rst.sort((a, b) => obj[a.str] - obj[b.str])

  return rst
}
Menu