title: Google Sheets Scripting sort by column then sort by column 2
body: <p>I'm trying to sort a range (3 columns total) by column 2 (Star grade) then by column 3 (level to level) then by column 1 (Bot), here's the current script I'm using to get it to sort by just column 2:</p>
  
  <pre><code>var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("DarkestSpawn");
  var range = sheet.getRange("AD12:AF51");
  var sortCol=1; // 0 for first column and 1 for second column and so on...
  var asc=false; // set variable asc to false for descending sort
  
  function onEdit(e) {
    sortrange();
  };
  
  function sortrange() {  
    var activeSheet = ss.getActiveSheet();
    var activeRange = activeSheet.getActiveRange();
    var sortedValues;
    if( sheet.getName() == activeSheet.getName() &amp;&amp;
      activeRange.getLastRow() &gt;= range.getRow() &amp;&amp; 
        activeRange.getRow() &lt;= range.getLastRow() &amp;&amp;
          activeRange.getLastColumn() &gt;= range.getColumn() &amp;&amp; 
            activeRange.getColumn() &lt;= range.getLastColumn() )
            { 
              sortedValues=range.getValues().sort(mySortFunction);
              range.setValues(sortedValues);
            }
  };
  
  var mySortFunction = function(a,b) {
    try{x=a[sortCol].toLowerCase();
        y=b[sortCol].toLowerCase();}
    catch(e){x=a[sortCol];y=b[sortCol];}
    return (x&gt;y)?(asc?1:-1):(x&lt;y)?(asc?-1:1):0
  };
  </code></pre>
  
  <p>This is example of a before, then after I want to do:</p>
  
  <p><a href="https://i.stack.imgur.com/64yo5.png" rel="nofollow noreferrer">Before</a>
  <a href="https://i.stack.imgur.com/6Auyb.png" rel="nofollow noreferrer">After</a></p>