title: How to sort range using Apps script based on a column
body: <p>I have this demo sheet
  <a href="https://docs.google.com/spreadsheets/d/1pGkTrudeDfv7Xkm7ZGM0fmQzSCqU05hFVhuczJ4cWCQ/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1pGkTrudeDfv7Xkm7ZGM0fmQzSCqU05hFVhuczJ4cWCQ/edit?usp=sharing</a></p>
  <p>I'm using this code to sort my data,</p>
  <pre><code>function SortColumns(){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(&quot;Ventas&quot;);
    var range = sheet.getRange(&quot;A4:S&quot;);
    var SORT_ORDER = [
  {column: 19, ascending: false},  
  {column: 18, ascending: false},
  {column: 13, ascending: false},
  {column: 7 , ascending: false},
  {column: 2 , ascending: true},
  {column: 1 , ascending: true},
  {column: 3 , ascending: true}
  ]
    range.sort(SORT_ORDER);
  }
  </code></pre>
  <p>which works ok</p>
  <p>What I need is:<br />
  if Column M contains 'Entregado' to sort in this way,<br />
  if contains 'Cobrar' sort in different order,<br />
  if contains 'Apartado' sort in different order,<br />
  if contains 'Preventa' sort in different order,<br />
  and if contains any other value to sort them in a different way</p>
  <p>is this possible?</p>
  <p>thanks!</p>
  <hr />
  <p>Edit: I'm trying this code, but its not working as expected</p>
  <pre><code>function ColumnContainString(column,string) {
    var column=column || 13;//used for initial testing
    var string=string || 'test';//used for initial testing
    var ss=SpreadsheetApp.getActive();
    var sheet=ss.getSheetByName(&quot;Ventas&quot;);
    var cA=sheet.getRange(4,column,sheet.getLastRow(),1).getValues();
    for(var i=0;i&lt;cA.length;i++) {
      if(cA[i][0].toString().indexOf(string)&gt;-1) {//you may wish to change this line to some regular expression to find only words
        Logger.log(i+1);
        return(i+1);
      }
    }
    Logger.log('-1');
    return -1;
  }
  
  function SortColumns(){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(&quot;Ventas&quot;);
    var range = sheet.getRange(&quot;A4:S&quot;);
   
   if(ColumnContainString(13,'Entregado')==-1) {
      var SORT_ORDER = [
      {column: 19, ascending: false},  // M
      {column: 18, ascending: true},   // H
      {column: 1 , ascending: true},   // Fecha
      {column: 2 , ascending: true},   // Cliente
      {column: 3 , ascending: true}    // Sabor
      ]   
        range.sort(SORT_ORDER);
    }
    
    if(ColumnContainString(13,'Cobrar')==-1) {
      var SORT_ORDER = [
      {column: 18, ascending: true},   // H
      {column: 13, ascending: true},   // Estatus
      {column: 7 , ascending: false},  // Vendio
      {column: 2 , ascending: true},   // Cliente
      {column: 3 , ascending: true},   // Sabor
      {column: 1 , ascending: true}    // Fecha
      ]  
        range.sort(SORT_ORDER);
    }
    
    if(ColumnContainString(13,'Pendiente')==-1) {
      var SORT_ORDER = [
      {column: 18, ascending: true},   // H
      {column: 13, ascending: true},   // Estatus
      {column: 7 , ascending: false},  // Vendio
      {column: 2 , ascending: true},   // Cliente
      {column: 3 , ascending: true},   // Sabor
      {column: 1 , ascending: true}    // Fecha
      ]  
        range.sort(SORT_ORDER);
    }
    
    if(ColumnContainString(13,'Preventa')==-1) {
      var SORT_ORDER = [
      {column: 18, ascending: true},   // H
      {column: 13, ascending: true},   // Estatus
      {column: 7 , ascending: false},  // Vendio
      {column: 2 , ascending: true},   // Cliente
      {column: 3 , ascending: true},   // Sabor
      {column: 1 , ascending: true}    // Fecha
      ]  
        range.sort(SORT_ORDER);
    }
    
    if(ColumnContainString(13,'Apartado')==-1) {
      var SORT_ORDER = [
      {column: 18, ascending: true},   // H
      {column: 13, ascending: true},   // Estatus
      {column: 2 , ascending: true},   // Cliente
      {column: 3 , ascending: true},   // Sabor
      {column: 1 , ascending: true}    // Fecha
      ]  
        range.sort(SORT_ORDER);
    }
  }
  </code></pre>