Sort by Field Name Instead of Column Letter in Google Sheets

When sorting data using the QUERY function in Google Sheets, you’re often required to use column letters (like A, B, or C) or column numbers. However, there’s a way to sort by field name instead of column letter, making your formulas more readable and adaptable to changes in column order.

Similarly, when using the SORT function, we typically specify column indexes. But with a clever workaround, you can also sort by field labels (i.e., column headers) instead of relying on hard-coded positions.

In this tutorial, I’ll walk you through how to sort by field name in both QUERY and SORT functions using XMATCH as the key trick.

Sample Data

Here’s a basic dataset (A:C) we’ll work with:

CategoryItemQty
VegetablesGreen Pepper1
VegetablesCarrot2
VegetablesCauliflower2
FruitsBanana5
VegetablesEggplant2
FruitsApple5
FruitsMango5
VegetablesAsparagus1
FruitsOrange10

Let’s sort this data by Category and Item.

Why Sort by Field Name Instead of Column Letter?

Sorting by field name instead of a column letter (like A or B) or a column number (1, 2) offers several key advantages:

  • Prevents formula breakage if column positions change
  • Improves readability and maintainability of your formulas
  • Simplifies column targeting in datasets with many fields
  • Feels more intuitive, especially in shared or complex spreadsheets

Sort by Field Name Instead of Column Letter in QUERY

Normally, you might write a QUERY like this:

=QUERY(A1:C, "select * where A is not null order by A asc, B asc", 1)

Step 1: Use column numbers (Col1, Col2) instead

=QUERY(A1:C, "select * where Col1 is not null order by Col1 asc, Col2 asc", 1)

Step 2: Reference column numbers dynamically

=QUERY(A1:C, "select * where Col1 is not null order by Col"&1&" asc, Col"&2&" asc", 1)

This isolates the column numbers so we can replace them dynamically using XMATCH.

Step 3: Replace column numbers with XMATCH for field names

=QUERY(A1:C, "select * where Col1 is not null order by Col"&XMATCH("Category", A1:C1)&" asc, Col"&XMATCH("Item", A1:C1)&" asc", 1)

Now your QUERY formula sorts by field names—not letters or static column numbers.

Example showing how to sort by field name instead of column letter in Google Sheets

Note: XMATCH searches the header row and returns the correct column number for the given field label.

Sort by Field Name Instead of Column Index in SORT

Using SORT, you typically do this:

=SORT(A2:C, 1, TRUE, 2, TRUE)

This sorts by the first and second columns in ascending order, but it’s rigid.

To sort by field label:

=SORT(A2:C, XMATCH("Category", A1:C1), TRUE, XMATCH("Item", A1:C1), TRUE)

Cleaner, and much more flexible.

Wrap UP

By using the XMATCH function, you can sort data dynamically based on field labels instead of hard-coded column letters or numbers. This technique is a great step toward more maintainable and readable formulas in Google Sheets.

If you’re working with evolving datasets or collaborating with others, this approach will save you time and avoid formula breakage due to column rearrangement.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Top N Products and Top N Sellers in Google Sheets (Top N of Top N)

If you’ve ever wanted to compare which sellers contribute the most to your top-selling...

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

Reset SCAN by Another Column in Google Sheets and Excel

Resetting SCAN function results based on values in another column is a topic of...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

More like this

Top N Products and Top N Sellers in Google Sheets (Top N of Top N)

If you’ve ever wanted to compare which sellers contribute the most to your top-selling...

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.