I have been working around for sorting an array of objects according to a property that doesn't exist in that array. I had my first array like
const scores = [
{
"scoreDefinitionName": "_mo45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573564",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "_sc45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573568",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "_ua45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573572",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "KFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573576",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "LFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573580",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "dummy",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573584",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "Score",
"score": 0.32,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573588",
"globalId": null,
"version": 0
}
}
]
and second array is like
[
{
variableName: "KFT",
variable: "KFT",
sequence: 1,
variableDetail: {
displayName: ""
},
},
{
variableName: "LFT",
variable: "LFT",
sequence: 3,
variableDetail: {
displayName: ""
},
},
{
variableName: "Dummy",
variable: "dummy",
sequence: 2,
variableDetail: {
displayName: ""
}
}
];
So bascially what I want to do is to sort the first array scores according to the sequence of that object which matches with object in second array varibles when the name property matches.
One work around I found is to first push the sequence property of variables array in the scores array and then sort according to that
But I believe it can be optimised more. Any other way to make it more optimise ?