title: How can I push an element into array at a sorted index position?
body: <p>Say I have an array already with these articles, ordered by lowest to highest price:</p>
  
  <pre><code>[
    { title: "Article 3", price: 1.49 },
    { title: "Article 1", price: 3.00 },
    { title: "Article 4", price: 5.99 },
    { title: "Article 2", price: 19.99 }
  ]
  </code></pre>
  
  <p>Basically I would like to push another article into the array at the correct position (by price). How can I do so?</p>
  
  <p>The new article I'm pushing may have the following properties:</p>
  
  <pre><code>{ title: "Article 5", price: 12.00 }
  </code></pre>
  
  <p>I'm expecting the article to appear at index 3 (between article 4 and 2).</p>
  
  <h1>UPDATE (SOLUTION)</h1>
  
  <p>I created a prototype method using @klutt's answer with binary search algorithm:</p>
  
  <pre><code>Array.prototype.pushSorted = function(el, compareFn) {
    this.splice((function(arr) {
      var m = 0;
      var n = arr.length - 1;
  
      while(m &lt;= n) {
        var k = (n + m) &gt;&gt; 1;
        var cmp = compareFn(el, arr[k]);
  
        if(cmp &gt; 0) m = k + 1;
          else if(cmp &lt; 0) n = k - 1;
          else return k;
      }
  
      return -m - 1;
    })(this), 0, el);
  
    return this.length;
  };
  
  const sortByPrice = (a, b) =&gt; a.price &gt; b.price;
  theArray.pushSorted({ title: "Article 5", price: 12.00 }, sortByPrice);
  </code></pre>