-1

I've got an array of object like this:

{"aid":1,"id":51,"modifier":5},
{"aid":2,"id":51,"modifier":5},
{"aid":3,"id":51,"modifier":2},
{"aid":4,"id":51,"modifier":3},
{"aid":5,"id":51,"modifier":3}

I will add an object {"aid":6,"id":51,"modifier":5} , the array now will look like this:

{"aid":1,"id":51,"modifier":5},
{"aid":2,"id":51,"modifier":5},
{"aid":3,"id":51,"modifier":2},
{"aid":4,"id":51,"modifier":3},
{"aid":5,"id":51,"modifier":3},
{"aid":6,"id":51,"modifier":5}

But i want to know is how i will push the new object {"aid":6,"id":51,"modifier":5} not into the last index of the array, but base on the modifier, that will look like this:

{"aid":1,"id":51,"modifier":5},
{"aid":2,"id":51,"modifier":5},
{"aid":6,"id":51,"modifier":5}
{"aid":3,"id":51,"modifier":2},
{"aid":4,"id":51,"modifier":3},
{"aid":5,"id":51,"modifier":3},

I dont know if it is possible, can somebdy solve this. Thanks

4
  • 3
    How is it sorted? 5 - 2 - 3? What way of sorting is that? Commented Aug 25, 2015 at 15:39
  • What is the exact logic? how can you decide 5 will go before the 2 and not after the 3? Or is it that you want the same modifier grouped together without a specific order? If so why does it hurt to sort it? Commented Aug 25, 2015 at 15:44
  • So you want to insert the new item after the last item with a similar modifier? That's all well and good, but then what happens next time? After aid:6 is added, and you reload the data, what do you want the list to look like? Commented Aug 25, 2015 at 15:44
  • Find the last index of your modifier and splice it? stackoverflow.com/questions/586182/… Commented Aug 25, 2015 at 15:47

2 Answers 2

2

You can pass a custom comparison function to Array.sort e.g

list.sort(function (a, b) {
  if (a.modifier > b.modifier) {
    return 1;
  }
  return -1;
});

I am not sure what the logic behind your search order is, but if you can write a a comparison function you can sort by it.

Sign up to request clarification or add additional context in comments.

Comments

0

I notice your Objects seem to be sorted by both modifier and aid. This means you want to sort by multiple properties. Write a comparator like this

function comparator(keys, a, b) {
    var k, key, mod = [1, -1], order;
    for (k = 0; k < keys.length; ++k) {
        key = keys[k].key;
        order = !keys[k].descending;
        if (b[key] > a[key]) return order ? -1 : 1;
        else if (b[key] < a[key]) return  order ? 1 : -1;
    }
    return 0;
}

Now say your Array is called foo, .bind the comparator with your key weightings as you pass it into foo.sort

foo.sort(
    comparator.bind(null, [
        {key: 'modifier', descending: true},
        {key: 'aid'},
    ])
);

2 Comments

i want to try this, but what are the key, a, b parameter? is, k is array, is b is value or?
@remolalata a and b are two elements, used by sort, keys is an Array of Objects you supply, such as the example shown :)

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.