-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

remo lalata
  • 203
  • 2
  • 3
  • 7
  • 3
    How is it sorted? 5 - 2 - 3? What way of sorting is that? – putvande Aug 25 '15 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? – user627283 Aug 25 '15 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? – Halcyon Aug 25 '15 at 15:44
  • Find the last index of your modifier and splice it? http://stackoverflow.com/questions/586182/how-do-i-insert-an-item-into-an-array-at-a-specific-index – user627283 Aug 25 '15 at 15:47

2 Answers2

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.

Andreas Møller
  • 839
  • 5
  • 9
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'},
    ])
);
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • i want to try this, but what are the key, a, b parameter? is, k is array, is b is value or? – remo lalata Aug 25 '15 at 16:31
  • @remolalata `a` and `b` are two elements, used by `sort`, `keys` is an _Array of Objects_ you supply, such as the example shown :) – Paul S. Aug 25 '15 at 16:38