PasteSheet icon PasteSheet logo mark — a spreadsheet grid with a curly brace on a green rounded square PasteSheet

Sort and order Google Sheets results

Ordering rows is the most common thing you do after filtering. Whether you want it inside the sheet or from your app, here is how to sort a Google Sheets query — including by date.

Last updated

Key facts

  • In the QUERY function, order by takes column identifiers (A, B) and sorts ascending unless you add desc. Clauses must also appear in a fixed order — select, where, group by, pivot, order by, limit. source
  • QUERY() is a spreadsheet formula: its result spills into the cells below it. It cannot serve sorted rows to an app or an AI agent. source
  • Over a REST endpoint the equivalent is ?sort=created&order=desc. Because PasteSheet infers column types when the sheet is connected, a date column sorts chronologically rather than as text — so order=desc genuinely means newest-first.

ORDER BY in the QUERY() function

Inside the sheet, add an ORDER BY clause (use DESC for newest-first):

Google Sheets
=QUERY(A1:D, "SELECT * ORDER BY D DESC")

sort & order over the API

From your app, sort with the sort and order parameters:

terminal
# newest first
curl 'https://pastesheet.com/api/your-endpoint-id?sort=created&order=desc'

# order by date, oldest first
curl 'https://pastesheet.com/api/your-endpoint-id?sort=joined&order=asc'

Ordering by date

Because PasteSheet infers column types, a date column sorts chronologically rather than as text — so order=desc on a date really means newest-first. Combine it with filters and paging for a full query, as covered in querying Google Sheets like SQL.

Frequently asked questions

How do I order a Google Sheets query?

Use ORDER BY in the QUERY() function inside the sheet, or the sort and order parameters (e.g. ?sort=created&order=desc) over the PasteSheet REST API.

How do I sort by date?

Sort on the date column. PasteSheet infers date types, so ordering is chronological — order=desc gives you newest-first.

Can I sort ascending and descending?

Yes. Use order=asc for ascending or order=desc for descending; QUERY() uses ASC/DESC.

Sources

Related guides

Turn your sheet into an API in minutes

Paste a Google Sheet URL and get a live REST API and MCP server — no backend, no code, free to start.