-4

There are data structure without indexes:

const data = [{
        "B": {
            "value": 1,
        },
        "D": {
            "value": "45"
        },
        "E": {
            "value": "234"
        },
        "A": {
            "value": "543"
        },
        "C": {
            "value": "250"
        }
    }, {
        "B": {
            "value": 6,
        },
        "D": {
            "value": "234"
        },
        "E": {
            "value": "67"
        },
        "A": {
            "value": "78"
        },
        "C": {
            "value": "12"
        }
    }
   ]

and array of strings by which data array above should be sorted:

const strings = ["E", "C", "B", "A", "D"];

Is there a solution to sort data array by strings in order to get final result like:

   [{
        "E": {
            "value": "234",
        },
        "C": {
            "value": "45"
        },
        "B": {
            "value": 1
        },
        "A": {
            "value": "543"
        },
        "D": {
            "value": "250"
        }
    }...
libik
  • 22,239
  • 9
  • 44
  • 87
corry
  • 1,457
  • 7
  • 32
  • 63

1 Answers1

-4

yes its actually pretty easy.

const data = [{
        "B": {
            "value": 1,
        },
        "D": {
            "value": "45"
        },
        "E": {
            "value": "234"
        },
        "A": {
            "value": "543"
        },
        "C": {
            "value": "250"
        }
    }, {
        "B": {
            "value": 6,
        },
        "D": {
            "value": "234"
        },
        "E": {
            "value": "67"
        },
        "A": {
            "value": "78"
        },
        "C": {
            "value": "12"
        }
    }
   ]

const strings = ["E", "C", "B", "A", "D"];

const sorted = data.map(obj => {
  const res = {};
  strings.forEach(key => res[key] = obj[key]);
  return res;
});

console.log(sorted);

However javascript does not guarantee the order of the variables inside objects, so you should not rely on it. To have it sorted, you have to use i.e. array, which will not be an exact same data structure, but it is similar and it will ensure the sorting.

const data = [{
        "B": {
            "value": 1,
        },
        "D": {
            "value": "45"
        },
        "E": {
            "value": "234"
        },
        "A": {
            "value": "543"
        },
        "C": {
            "value": "250"
        }
    }, {
        "B": {
            "value": 6,
        },
        "D": {
            "value": "234"
        },
        "E": {
            "value": "67"
        },
        "A": {
            "value": "78"
        },
        "C": {
            "value": "12"
        }
    }
   ]

const strings = ["E", "C", "B", "A", "D"];

const sorted = data.map(obj => {
  const res = [];
  strings.forEach(key => {
    const newObj = {};
    newObj[key] = obj[key];
    res.push(newObj)
  });
  return res;
});

console.log(sorted);
libik
  • 22,239
  • 9
  • 44
  • 87