I have a Javascript array that has 2 properties id, sortedPosition.
I want to kind of fake sort the array based on id, and modify sortedPosition such that it reflects the sorted position of the object within that array.
For example:
Input array:
[
{
"id" : 34,
"sortedPosition" : 2
}, {
"id" : 3,
"sortedPosition" : 1
}, {
"id" : 344,
"sortedPosition" : 0
}
]
Output array:
[
{
"id" : 34,
"sortedPosition" : 1
}, {
"id" : 3,
"sortedPosition" : 0
}, {
"id" : 344,
"sortedPosition" : 2
}
]
I came upon a solution which looks pretty bad, it involves 2 extra copies of deepCloned arrays, and it doesn't seem right. There has to be a more elegant solution.
Thanks for any help.