Google Sheets – How to Sort Two Columns

google sheetsgoogle-apps-script

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?

Best Answer

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