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)
- Open your sheet and choose Extensions > Apps Script.
- Delete what is in
Code.gsand paste the script below. - 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.
- Click Save. That's it:
onEditis 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
- Only edits made by people trigger it. Rows written by another script, the Sheets API, or tools like Zapier and Make do not fire
onEdit, so those rows stay where they land until the next manual edit. - One block per tab. The script sorts everything below the header as one range. Two tables stacked on one tab get mixed together.
- Merged cells in the range make the sort fail (you will see the error under Extensions > Apps Script > Executions).
- No custom order. It sorts A to Z or Z to A; "High, Medium, Low" needs a helper column with numbers.
- Simple triggers stop after 30 seconds, which is fine for normal sheets but not for very large ones.
5. Other options, compared
| Option | Sorts the real rows? | Keeps sorting as rows are added? |
|---|---|---|
| Data > Sort range | Yes | No, once |
=SORT(Sheet1!A2:F, 1, TRUE) on another tab | No, a read-only copy | Yes |
| Filter views, table views | No, only your view | Partly (the view) |
| The script above | Yes | Yes, for edits by people (and forms with step 3) |
| An add-on such as KeepSorted | Yes | Yes, 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.
The script on this page stays free either way.
More KeepSorted guides
- Auto sort script generator
- Auto-sort alphabetically (A to Z)
- Auto-sort by date, newest first (and Google Forms)
- Auto-sort by multiple columns, when data changes
- StaySorted alternative: an honest comparison
Sources
- Apps Script: simple triggers (onEdit, 30-second limit, script and API changes do not fire triggers)
- Apps Script: installable triggers (onFormSubmit)
- Range.sort reference
- SORT function