KeepSorted

How to auto-sort a Google Sheet when a new row is added

Google Sheets has no setting that keeps rows in order. Data > Sort range sorts once, and the SORT() formula makes a sorted copy somewhere else. To keep the real rows sorted every time someone adds or changes a row, you need a small Apps Script (free, below) or an add-on. Checked against Google's documentation on 1 November 2026.

1. The free script (5 minutes)

  1. Open your sheet and choose Extensions > Apps Script.
  2. Delete what is in Code.gs and paste the script below.
  3. Change the four settings at the top: the tab name, the column to sort by (A = 1, B = 2, ...), the direction, and how many header rows stay on top.
  4. Click Save. That's it: onEdit is a simple trigger, so it runs on its own whenever someone edits the sheet, with no authorization step.
// Keeps one tab sorted whenever someone edits it.
const SHEET_NAME = 'Sheet1';  // tab to keep sorted
const SORT_COLUMN = 1;        // column to sort by: A = 1, B = 2, C = 3 ...
const ASCENDING = true;       // false = Z to A, largest or newest first
const HEADER_ROWS = 1;        // rows at the top that never move

function onEdit(e) {
  const sheet = e.range.getSheet();
  if (sheet.getName() !== SHEET_NAME) return;
  sortSheet_(sheet);
}

function sortSheet_(sheet) {
  const rows = sheet.getLastRow() - HEADER_ROWS;
  if (rows < 2) return;
  sheet.getRange(HEADER_ROWS + 1, 1, rows, sheet.getLastColumn())
       .sort({column: SORT_COLUMN, ascending: ASCENDING});
}

To sort by two columns (say status, then date), replace the .sort(...) line with
.sort([{column: 3, ascending: true}, {column: 1, ascending: false}]);

2. Stop rows jumping while you type

With the script above, a new row moves the moment you type its first cell, before you have filled in the rest. To sort only when the sort column itself changes, add this line as the first line inside onEdit, and fill in the sort column last:

  if (e.range.getColumn() > SORT_COLUMN || e.range.getLastColumn() < SORT_COLUMN) return;

3. Rows from Google Forms

A form response is not an edit, so onEdit does not run for it. Add these two functions under the script, choose installFormTrigger in the toolbar and click Run once (Google asks you to authorize the script). After that every form submission sorts the tab. Try it on a copy of the sheet first.

function sortAfterForm() {
  sortSheet_(SpreadsheetApp.getActive().getSheetByName(SHEET_NAME));
}

function installFormTrigger() {
  ScriptApp.newTrigger('sortAfterForm')
    .forSpreadsheet(SpreadsheetApp.getActive())
    .onFormSubmit()
    .create();
}

4. The catches

5. Other options, compared

OptionSorts the real rows?Keeps sorting as rows are added?
Data > Sort rangeYesNo, once
=SORT(Sheet1!A2:F, 1, TRUE) on another tabNo, a read-only copyYes
Filter views, table viewsNo, only your viewPartly (the view)
The script aboveYesYes, for edits by people (and forms with step 3)
An add-on such as KeepSortedYesYes, set up from a sidebar

6. No code: KeepSorted

KeepSorted is the add-on version of this page: pick the area and the sort columns in a sidebar and it keeps the rows sorted, header on top, blanks last, formatting and formulas moving with their rows. Free: one rule per tab with up to 2 sort columns, with no trial clock. Pro ($5/month or $39/year): stacked tables sorted separately, custom order such as High > Medium > Low, up to 5 columns, and rows from Google Forms and other editors.

KeepSorted is built and in testing. It is not in the Google Workspace Marketplace yet. Leave your email and we send one message when you can install it.

You're on the list. We'll email you when KeepSorted is ready.

The script on this page stays free either way.

More KeepSorted guides

Sources