Web Applications Stack Exchange is a question and answer site for power users of web applications. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I've found the following answer in regards to auto-sorting by one column, which works great How can I make some data on a Google Spreadsheet auto-sorting?

However, my use case requires the sorting of two columns. One column is whole numbers, while the second column are dates. I would like to sort the whole numbers column first, then within each number sort the date.

Any suggestions?

share|improve this question
up vote 6 down vote accepted

Starting with the following line of code:

var range = sheet.getRange("A1:B10");

The sort can be performed in different ways:

  1. range.sort([1,2]);
  2. range.sort([{column: 1, ascending: true}, {column: 2, ascending: true}]);

Both ways are identical as the sort type is by default ascending, but the second option allows for different sort order when TRUE is changed to FALSE.

See reference: range.sort

share|improve this answer
1  
Thanks for the quick response. Your answer makes sense to me. I've implemented the knowledge with success. – Viet Apr 16 '13 at 18:27
    
OK, new to this stuff...where exactly do I enter the above code? – Robb May 4 at 4:31
    
@robb see reference the OP uses, that Will lead to An answer I gave with explanation. – Jacob Jan Tuinstra May 4 at 7:52

A bit late to the party but this is what I did.

function onEdit(event){
  var sheet = event.source.getActiveSheet();
  if(sheet.getName()=='Sort by country full'){
    var editedCell = sheet.getActiveCell();
    var columnToSortBy = 16;
    var columnToSortBy2 = 2;
    var tableRange = "A2:AU1451";
    if(editedCell.getColumn() == columnToSortBy){   
      var range = sheet.getRange(tableRange);
      range.sort( [{ column : columnToSortBy },{column: columnToSortBy2,ascending: false }] );
    }
  }
}

Sorts by column 16 first which for me is countries and then by a scoring rating in column 2 descending.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.