All posts
Blog

Sorting Google Sheets Data via REST API

How to sort rows returned by SheetsAPI using the sort query parameter — ascending, descending, and multi-field sorting patterns.

3 min read

When you fetch a sheet through SheetsAPI, rows come back in the order they appear in the spreadsheet — top to bottom, exactly as a human would see them. For many use cases that is fine. For others — a product catalog sorted by price, a leaderboard sorted by score, a directory sorted alphabetically — you need to control the order at the API level.

SheetsAPI handles this with a single sort query parameter.

Ascending sort

To sort by a field in ascending order, pass the field name as the sort value:

curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Products?sort=price"

This returns rows sorted by the price column, lowest value first. The field name must match the column header in your sheet exactly, including case.

In JavaScript:

const res = await fetch(
  "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Products?sort=price"
);
const { data } = await res.json();
// data is sorted by price ascending

Descending sort

Prefix the field name with a minus sign (-) to reverse the order:

curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Products?sort=-price"

Highest price first. The minus prefix is the only syntax difference between ascending and descending — there is no desc keyword or separate parameter.

# Sort a leaderboard by score, highest first
curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Scores?sort=-score"
 
# Sort a directory alphabetically by last name
curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Directory?sort=last_name"
 
# Sort an event list by date, most recent first
curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Events?sort=-date"

One field at a time

SheetsAPI sorts on a single field per request. There is no multi-field sort syntax like sort=last_name,first_name. If you pass two comma-separated values, the behavior is undefined — do not rely on it.

When you need to sort by multiple fields, the cleanest approach is to fetch the data and sort in JavaScript after the response arrives:

const res = await fetch(
  "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Directory?sort=last_name"
);
const { data } = await res.json();
 
// Secondary sort: within the same last name, sort by first name
data.sort((a, b) => {
  const lastCmp = a.last_name.localeCompare(b.last_name);
  if (lastCmp !== 0) return lastCmp;
  return a.first_name.localeCompare(b.first_name);
});

The API handles the primary sort; JavaScript handles the tiebreaker. This keeps the network payload in the right order without downloading the entire sheet unsorted.

Sorting with other parameters

sort composes with the rest of SheetsAPI's query parameters. Common combinations:

# Top 10 products by price, only name and price fields
curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Products?sort=-price&limit=10&fields=name,price"
 
# Search for a category, then sort results alphabetically
curl "https://sheetsapi.gkit.mreshank.com/api/spreadsheets/YOUR_KEY/Products?search=category:Audio&sort=name"

The search filter is applied first, then the sort is applied to the filtered rows, then limit and offset are applied. This means limit=10&sort=-price gives you the 10 highest-priced items, not 10 random items sorted by price.

Numeric vs. string sort

SheetsAPI sorts string values lexicographically by default. This is correct for text fields but produces surprising results for numbers stored as strings: "9" sorts after "10" because "9" > "1" character by character.

If your sheet stores numbers as actual numbers (not formatted as text), the sort will be numeric and correct. If you are seeing unexpected order on a numeric column, check the cell format in Google Sheets — select the column and look at the format selector in the toolbar. Cells showing left-aligned values are usually being treated as text.

The safest fix is to ensure the column is formatted as Number in Sheets. You can also work around it by sorting client-side with parseFloat:

data.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));

For more on what SheetsAPI returns and how field types work, see Filtering and Searching Google Sheets Data.

Share