2

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.

2 Answers 2

3

I came upon a solution which looks pretty bad, it involves 2 extra copies of deepCloned arrays

A shallow copy of the array should be enough:

arr.slice() // create new array with the same objects
 .sort(function(a,b){return a.id-b.id;}) // sort that (leave original untouched)
 .forEach(function(o,i){o.sortedPosition = i;}); // update the objects
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, your elegant solution is exactly the reason why I summoned SO's help.
0

Sort the array and then update the position.

array.sort(function(a1, a2) {
  return a1.id - a2.id;
});

array.forEach(function(v, i) {
  v.sortedPosition = i;
});

edit if you just want to set the sorted position, you can do something like this:

var t = array.map(function(v) {
  return { v: v }
});
t.sort(function(v1, v2) { return v1.v.id - v2.v.id; });
t.forEach(function(v, i) { v.v.sortedPosition = i; });

That makes a new array with references to the objects in the original array, then sorts it, then fixes up the position properties. The ordering of the original array is unaffected. The pass to make a new array won't be very expensive; it's not really a "deep copy" at all.

2 Comments

But this thing is that I don't want the array to be sorted. Just wish to update sortedPosition.
I updated the order of objects in question to avoid this confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.