title: how to sort a 2D array in google apps script/ google spreadsheet
body: <p>I have a google spreadsheet which contains the multiple data spread across various columns (like 6 columns - Phone number, IMEI, URL, Id, Reg No and Time) and i have 15K rows of data.</p>
  
  <p>I have to find/show all the duplicate phone number(Column 0) and data associated with them in a new sheet.</p>
  
  <p>However when i try to sort the data, it throws me error like</p>
  
  <p>"Comparison method violates its general contract. (line 6, file "Code")" </p>
  
  <p>The code is shown below for your reference:</p>
  
  <pre><code>function myFunction() {
  
    var values  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('DUP').getDataRange().getValues()
  
    var dupData = new Array();
  
    values.sort(function sortFunction(a, b) {
      if (a[0] === b[0]) {
          return 0;
      }
      else {
          return (a[0] &lt; b[0]) ? -1 : 1;
      }
  });
  
    for(i=1;i&lt;values.length;i++)
    {
      if (values[i][0] == values[i+1][0]) {
          dup.push(values[i]);
      }
    }
  
     var new1 = dpnum.getSheetByName('Test123');
    new1.getRange(1,1,dup.length,dup[0].length).setValues(dup);
  
  }
  </code></pre>