It seems easy enough but when I think of how I would write it, it turns into some big mess and I want it to be as efficient as possible (within a reasonable amount at least). I also have two questions pertaining to this problem.
1) So I have an array of objects that look like this
[{id: apple, position: 0},{id: orange, position: 1},{id: banana, position: 2}]
But the position is not guaranteed to be in order, like this
[{id: apple, position: 2},{id: orange, position: 0},{id: banana, position: 1}]
And I'd like the algorithm to loop through the array and grab the id's in ascending order based on the positions, naturally I'd want to do something with the id I receive in the loop, so I could have some sort of loop and within the loop print out 'I ate' + id so it would come out as
I ate orange
I ate banana
I ate apple
2) Think of this as a different scenario but using the same data structure as before
[{id: apple, position: 0},{id: orange, position: 1},{id: banana, position: 2}, {id: grape, position: 4}, {id: mango, position: 3}]
And I take out the object with id orange so it looks like this
[{id: apple, position: 0},{id: banana, position: 2}, {id: grape, position: 4}, {id: mango, position: 3}]
But now the positions don't make sense because there's 0 -> 2 -> 3 -> 4 but no position 1, so I'd like to put them back in proper ascending order (It must hold the same sequence, so it still has to go apple -> banana -> mango -> grape) so it turns into
[{id: apple, position: 0},{id: banana, position: 1}, {id: grape, position: 3}, {id: mango, position: 2}]
How would I do this best as well?
Note: There could be a scenario where I have to take out more than one object, but I guess it doesn't matter since I can just sequentially do it or something.
Thanks!