title: JavaScript sort comparator function
body: <p>Basically I want to build a function which sorts objects in an array by one of the object's properties/member variables. I am preeeety sure that the comparator function is where the error is hidden, but I am not 100% sure.</p>
  
  <p>The output I should get after the sort function is called is <code>1,2,3</code>. I get <code>1,3,2</code> which means that it is unchanged</p>
  
  <p>This is the entire js code (with some comments):</p>
  
  <pre><code>var arr = [];
  //object definition and creation
  var main = document.getElementById("main");
  var task = {
      name: "",
      priority: 0
  };
  
  //first
  var one = Object.create(task);
  one.priority = 1;
  //secondd
  var two = Object.create(task)
  two.priority = 3;
  //last
  var three = Object.create(task);
  three.priority = 2;
  
  //append
  arr.push(one);
  arr.push(two);
  arr.push(three);
  
  //sort function
  function sortT() {
      arr.sort(compareFN);
  }
  
  //comperator function
  function compareFN() {
      return task.priority &lt; task.priority;
  }
  
  function print() {
      for (var i = 0; i &lt; arr.length; i++) {
          console.log(arr[i].priority);   
      }
  }
  
  //execution of the program
  print();
  sortT();
  print();
  </code></pre>
  
  <p>EDIT: The solution is the following - As stated, the comparator function really was the problem, the correct way to write it is the following:</p>
  
  <pre><code>function compareFN(taskA, taskB) {
     return taskA.priority &lt; taskB.priority;
  }
  </code></pre>