I was wondering how I could re-order an array so that each object is at the index of its own id
- 1.
//Lets say we have an array like so
var unOrderedArray=[{Id:2,Name:"Bob"}, {Id:1,Name:"Julian"},
{Id:3,Name:"Jeff"}]
What kind of function could i write so that I get:
var orderedArray=[{Id:1,Name:"Julian"},{Id:2,Name:"Bob"},{Id:3,Name:"Jeff"}]
so far I have tried this but it doesn't work:
for(let i = 0;i<unOrderedArray.length;i++){
correctlyOrderedArray[unOrderedArray[i].Id]=unOrderedArray[i];
}
P.S! It is not assured that the lowest Id is 1 and that there are no gaps in between the lowest and highest Id, for example 2,10,5,3 etc.