So I'm using a script that is sorting my rows from the lowest number to the highest but I want it to do upside down, gonna try to explain it better, the script now sort the rows like this

enter image description here

And I want it to sort the rows instead of 1, 3, 4, 33, 34, 65... to 65, 34, 33, 4, 3, 1.

Here is the script I'm using right now.

enter image description here

text equivalent (converted at Tesseract.js)

function onEdit(event){
  var sheet = SpreadsheetApp.getActivesheet();
  var editedCell = sheet.getActiveCell();

  var columnToSortBy = 4;
  var tableRange = "B5:F40";

  if(editedCell.getColumn() == columnToSortBy){
    var range = sheet.getRange(tableRange);
    range.sort({ column : columnToSortdy});

  • 1
    Welcome to Web Applications. Code should be added as text not as image, also please show what you tried to adapt the script to your needs and a brief description of your search efforts as is suggested in How to Ask. Apr 29, 2021 at 21:26
  • Welcome. You've obviously found the method that allows you to sort a range. Have you not read/looked at the relevant documentation? As well as describing the range to sort, you can also specify whether the result is sorted in ascending (default) or descending order. The documentation includes examples.
    – Tedinoz
    Apr 29, 2021 at 22:56

1 Answer 1


The onEdit() autosort function you quote seems to date back to 2014. The primary problem with it is that it does not check which sheet the edit took place on, so it will happily re-order data on any sheet where you happen to edit column D.

You should add a couple of rudimentary checks so that the function only sorts ranges in sheets where it is supposed to. It would not hurt being able to specify more than one range to sort either. The user should also be notified of the sort so that its ghostly action does not come as a surprise. Try this:

function onEdit(e) {
  // @see https://webapps.stackexchange.com/questions/153999
  try {
    var sortSpecs = [
        range: e.source.getRange('Sheet1!B5:F40'),
        column: 4, // column D = 4
        ascending: false, // true: ascending, false: descending
        range: e.source.getRange('Another sheet!A2:C'),
        column: 1,
        ascending: true,
  } catch (error) {
      'Unknown sheet or incorrect range in sortSpecs in line '
      + String(error.stack).match(/\d+:/).join(', ').slice(0, -1),
  sortSpecs.some(spec => {
    if (e.range.getSheet().getSheetId() !== spec.range.getSheet().getSheetId()
      || e.range.columnStart > spec.column
      || e.range.columnEnd < spec.column
      || e.range.rowStart < spec.range.getRow()
      || e.range.rowEnd > spec.range.getLastRow()) {
      return false;
    spec.range.sort({ column: spec.column, ascending: spec.ascending });
    e.source.toast('Sorted range ' + spec.range.getA1Notation() + '.', 'Autosort');
    return true;

Edit the sortSpecs object array to add or delete ranges to sort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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