3

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.

  • 1
    Possible duplicate of [Sorting an array of JavaScript objects](https://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – shukshin.ivan Aug 27 '17 at 12:41

4 Answers4

6

Use sort method. No matter which Ids you have.

var unOrderedArray=[{Id:21,Name:"Bob"}, {Id:5,Name:"Julian"},
    {Id:313,Name:"Jeff"}, {Id:4,Name:"Ivan"}];

orderedArray = unOrderedArray.sort(function(a, b) {
   return a.Id - b.Id;
});
console.log(orderedArray);
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • 1
    That just sorts the array from lowest Id to highest, but in my case there might not be every id in between the highest and lowest, for example we might not have the {Id:2,Name:"Bob"} object in there and then not every object inside the array wont be in the index of its id-1. Big thanks anyway ! Will update the question so it is better explained. – Albert Koppelmaa Aug 27 '17 at 13:46
  • `-` operator just compares two values, you don't need to have all the Ids, you can have say 5, 3, 7. Have you tried the code for your case? – shukshin.ivan Aug 27 '17 at 14:39
  • 1
    I've understood, you needed `undefined`s for missed elements. – shukshin.ivan Aug 28 '17 at 13:08
1

You can use Array#reduce to create an array with the items placed according to their ids. However, if the ids are not continuous, you'll get a sparse array (an array with holes - undefineds).

var unOrderedArray=[{Id:2,Name:"Bob"}, {Id:1,Name:"Julian"}, {Id:3,Name:"Jeff"}, {Id:10,Name:"Tom"}];

var orderedArray = unOrderedArray.reduce(function(arr, item) {
  arr[item.Id - 1] = item;

  return arr;
}, []);

console.log(orderedArray); // since the Tom's id is 10, you'll get 6 undefined items between Tom and Jeff
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

Actually, as arrays are zero based, one would do:

 const result = [];
 for(var obj of unOrderedArray){
  result[obj.Id] = obj;
 }

Which results in

[undefined, { Id:1,Name:"Bob"},...]

So one can do

result[1]

to get the object with id one.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

If you're not sure whether the IDs will be perfectly sequential 1-N, you could use this:

const sortById = arr => {
  if (!arr.length) return arr;
  const max = Math.max(...arr.map(obj => obj.Id));
  let result = Array(max).fill(null);
  arr.forEach(obj => result[obj.Id] = obj);
  return result;
};
Zevgon
  • 556
  • 4
  • 13