Auto-sort a Google Sheet alphabetically, every time you add a name
Data > Sort range > A to Z puts a list in order once. The next name you type lands at the bottom again. To keep a list of names, products or clients in alphabetical order on its own, you need a short script or an add-on. This page has a free script, the rules Google Sheets uses for A to Z (case, blanks, numbers), and how to sort by last name. Checked against Google's documentation on 1 November 2026.
1. The free script: A to Z on every edit
- Open the sheet, choose Extensions > Apps Script, and replace
Code.gswith the script below. - Set the tab name, the column to sort by (A = 1, B = 2...) and the number of header rows.
- Click Save. From now on every edit on that tab puts the rows back in A to Z order. Whole rows move together, so a name keeps its email and phone number.
// Keeps a tab in alphabetical order (A to Z) whenever someone edits it.
const SHEET_NAME = 'Sheet1';
const SORT_COLUMN = 1; // column with the names: A = 1, B = 2 ...
const HEADER_ROWS = 1; // rows at the top that never move
const FIRST_COLUMN = 1; // first column that moves with the names
const LAST_COLUMN = 0; // last column that moves; 0 = last column with data
function onEdit(e) {
const sheet = e.range.getSheet();
if (sheet.getName() !== SHEET_NAME) return;
// Sort only when the name column was edited, so rows don't jump while you type.
if (e.range.getColumn() > SORT_COLUMN || e.range.getLastColumn() < SORT_COLUMN) return;
sortAlphabetically_(sheet);
}
function sortAlphabetically_(sheet) {
const rows = sheet.getLastRow() - HEADER_ROWS;
if (rows < 2) return;
const last = LAST_COLUMN || sheet.getLastColumn();
sheet.getRange(HEADER_ROWS + 1, FIRST_COLUMN, rows, last - FIRST_COLUMN + 1)
.sort({column: SORT_COLUMN, ascending: true}); // false = Z to A
}
Type the other cells of a new row first and the name last: the row moves into place when the name is entered. To sort a single list and leave the columns next to it alone, set FIRST_COLUMN and LAST_COLUMN to the list's own column. Need other options (several columns, Forms, a timer)? The auto sort script generator writes a script for your exact range.
2. How Google Sheets orders A to Z
| Case | What happens | Fix, if you need one |
|---|---|---|
| Upper and lower case | Ignored: apple, Banana, cherry, not Banana, apple, cherry. | None needed for names; Sheets' sort has no case-sensitive option. |
| Empty cells | Always last, in A to Z and in Z to A. | Nothing to do: blank rows never float to the top. |
| Numbers and dates in the column | Come before text in A to Z. | Keep one kind of value per column. |
| Numbers typed as text ("10", "9") | Sorted letter by letter: "10" before "9". | Make them real numbers (Format > Number, retype) or pad with zeros (09, 10). |
| A leading space (" Zoe") | The space counts, so the name jumps to the top. | Data > Data cleanup > Trim whitespace, or =TRIM(A2). |
| "The Beatles", "A Tribe..." | Sorted under T and A. | A helper column without the article, e.g. =REGEXREPLACE(A2, "(?i)^(the|a|an) ", ""), and sort on that. |
The script and the KeepSorted add-on follow these same rules, because both use Google's own sort.
3. Sort by last name
If column A holds full names ("Ada Lovelace"), add a helper column with the last word, for example in G2: =IFERROR(REGEXEXTRACT(TRIM(A2), "[^ ]+$"), ""), fill it down, and sort by the helper first and the full name second. In the script, replace the .sort(...) line with:
.sort([{column: 7, ascending: true}, {column: 1, ascending: true}]); // G (last name), then A
The helper formula has to be in every row, so copy it down far enough (or into each new row). Rows with an empty helper go last. Two-word surnames ("van Gogh") need a manual entry in the helper.
4. Without a script
| Option | Stays alphabetical? | Catch |
|---|---|---|
| Data > Sort range > A to Z | No, once | Repeat after every change. |
=SORT(A2:C, 1, TRUE) on another tab | Yes | A read-only copy; you can't type notes next to it that stay with their row. |
=SORT(FILTER(A2:A, A2:A<>"")) | Yes | Same, for a single list without blanks. |
| Filter views and table views | Only in your view | Other people still see the unsorted rows. |
| The script above | Yes, on every edit | Rows from Forms, Zapier or the API wait for the next edit (the generator adds triggers for those). |
| KeepSorted add-on | Yes | Not in the Marketplace yet (below). |
5. No code: KeepSorted
KeepSorted is a Google Sheets add-on: select the list or table, pick the column, choose A to Z, Save. It keeps the real rows in that order, header on top, blanks last, case ignored, formatting and formulas moving with their rows. Free: one area per tab, up to 2 sort columns (last name, then first name), no trial clock. Pro ($5/month or $39/year): up to 5 sort columns, custom orders, stacked tables sorted separately, and sorting after form responses and other editors' changes.
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 scripts on this page stay free either way.
More KeepSorted guides
- Auto sort script generator
- Auto-sort when a new row is added
- Auto-sort by date, newest first (and Google Forms)
- Auto-sort by multiple columns when data changes
- StaySorted alternative: an honest comparison