title: Sort multidimensional object literal array by external value
body: <p>I am trying to make a homemade line of sight for a game im building which is a gridbased, 2D top-view board game. The board has divs in a 2-dimensional array which are all black and will be transparent if the player is in a corridor which are also in a 2-dimensional array with objects. There are miscellaneous blocks in the corridors and i need to take them into consideration so the divs will stop getting transparent if they appear in the corridors where the player is.</p>
  
  <p>So i've got an idea to sort the array of objects in a given corridor where the player is located by the x,y values.</p>
  
  <pre><code>corridors[i].sort(
      function(a)
      {
          if(a.y &gt; playerObj.position.y &amp;&amp; a.x &gt; playerObj.position.x) return 1;
          if(a.y &lt; playerObj.position.y &amp;&amp; a.x &lt; playerObj.position.x) return -1;
          return 0;
      });
  </code></pre>
  
  <p>But this does not seem to work. The changing of the divs background-color to transparent is still beginning from the array's first index. I want it to start from the players position in the corridor and work itself outwards like so:</p>
  
  <p>O is simply a tile, P is the player, X is the tile that has just been made transparent</p>
  
  <p>OOOOOOOOOOOPOOOOOO</p>
  
  <p>OOOOOOOOOOXPOOOOOO</p>
  
  <p>OOOOOOOOOOXPXOOOOO</p>
  
  <p>OOOOOOOOOXXPXOOOOO</p>
  
  <p>But for this scenario to function i need to find how to sort my array correctly, and i hope you can help me with this. The examples i've seen on the net has parameters a and b, so is it not possible to use an external variable? Should i perhaps create the players position as a new object and push it to the array and then sort the array?</p>