Considering the JSON structure bellow, I need to be able to change the position of an item. The position can be changed only in increments of 1 (so +1 or -1).
Given the fact that I always send back to the API the entire JSON and that there can be a few hundreds items in the structure, is there a known algorithm to achieve this positions change fast?
The item is searched in the structure by its id.
For example I want to update the "position" of item with "id = 2" to "1". This step is easy because I can loop through the structure and find the element by id. But where I got stuck is the step where I need to update the other items accordingly.
I'm looking to implement this in PHP or Javascript.
{
"employees": {
"employee": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"position": 1
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"position": 2
},
{
"id": "3",
"firstName": "Robert",
"lastName": "Downey Jr.",
"position": 3
}
]
}
}