How to Sort Horizontally (Columns Left to Right) in Google Sheets

Published on

Unlike Excel, Google Sheets does not have a built-in menu command to sort data horizontally. In Excel, users can rely on the SORT menu command to sort both top to bottom and left to right. However, Google Sheets currently supports sorting only from top to bottom.

To sort horizontally (columns from left to right), we can use the SORT or SORTN function with TRANSPOSE in Google Sheets.

Another function that offers the sorting feature is QUERY. However, I don’t recommend it for left-to-right sorting, as changing the data orientation using TRANSPOSE might invite mixed data type columns. In such columns, QUERY considers minority data types as null values for querying purposes. So, the result won’t be perfect.

Let me help you understand the term horizontal sorting with an example.

For instance, if you have the names of students across the row in a data range and you want their names to be arranged alphabetically, you can sort columns from left to right.

Why Horizontal Sorting Matters in Google Sheets

Here are some points that explain why sorting columns from left to right is important in Google Sheets.

Enhanced Lookups: Sorting horizontally can significantly impact the performance of lookup functions that support horizontal lookup in Google Sheets, including HLOOKUP, LOOKUP, MATCH, XMATCH, and XLOOKUP.

Effortless Category Comparisons: Compare values across categories (columns) effortlessly with horizontal sorting. Analyze trends, spot variances, and gain deeper insights from your data in a flash.

Effortless Comparisons: Horizontal sorting makes it easier to identify trends and outliers in statistics like rank, quartiles, and percentiles. By visually comparing values across rows, you can gain valuable insights without relying solely on dedicated functions.

Formulas to Sort Data Horizontally in Google Sheets

Let’s use basic data in the range B3:H4 for the examples.

In the following sample data, we have names in C3:H3 and their corresponding ages in C4:H4. B3:B4 contains the labels “Name” and “Age”. Let’s explore how to sort horizontally, first by name and then by age.

Sample Data: Sorting Columns Left to Right in Google Sheets

The above-mentioned sort functions (SORT and SORTN) sort data from top to bottom. Therefore, we will use the TRANSPOSE function to change the orientation of the data and then apply the sorting functions. Once sorted, we will use TRANSPOSE again to restore the original data orientation.

Generic Formula to Sort Data From Left to Right in Google Sheets:

TRANSPOSE(sort_formula(TRANSPOSE(data), …))

Sorting Columns Left to Right Using SORT Function

Syntax of the SORT Function:

SORT(range, sort_column, is_ascending, [sort_column2, …], [is_ascending2, …])

Formula:

=TRANSPOSE(SORT(TRANSPOSE(C3:H4)))

This formula is equivalent to sorting the range C3:H4 by C3:H3 (the first row) in ascending order.

The first column of the range B3:B4 contains labels. I’ve excluded that from the sort. Once horizontally sorted, the required data can be stacked with the result horizontally using HSTACK:

Generic Formula:

=HSTACK(label_range, sort_formula)

In the above example, the formula will be as follows:

=HSTACK(B3:B4, TRANSPOSE(SORT(TRANSPOSE(C3:H4))))
Sorting Data Horizontally and Appending Labels

To sort first by age and then by name, you need to use the other parameters in the SORT function.

In the function, they are sort_column, is_ascending, [sort_column2, …], [is_ascending2, …]. You should read it concerning left-to-right sorting. So, read sort_row, is_ascending, [sort_row2, …], [is_ascending2, …].

Formula:

=TRANSPOSE(SORT(TRANSPOSE(C3:H4), 2, TRUE, 1, TRUE))

This formula will horizontally sort the range by age in ascending order and then by name in ascending order.

To horizontally sort the data in descending order, you should specify FALSE instead of TRUE for is_ascending.

Sorting Columns Left to Right Using SORTN

The SORTN function follows the same generic formula, which is TRANSPOSE(sort_formula(TRANSPOSE(data), …)). You can use this generic formula to obtain n sorted columns when utilizing SORTN in the sort_formula.

Use the following formula to sort the names horizontally and extract the first three names.

=TRANSPOSE(SORTN(TRANSPOSE(C3:H4), 3))

In this formula, 3 represents ‘n’, which is the number of columns to return.

This adheres to the SORTN syntax SORTN(range, [n]).

Similar to SORT, we can use HSTACK to stack the labels with the horizontally sorted result. Please refer to this screenshot.

Sorting Left to Right and Extracting N Columns with Labels

To sort the horizontal range by age (second row) and then by name (first row), use the following SORTN formula:

=TRANSPOSE(SORTN(TRANSPOSE(C3:H4), 3, 0, 2, TRUE, 1, TRUE))

This adheres to the SORTN syntax SORTN(range, [n], [display_ties_mode], [sort_column], [is_ascending], [sort_column2, …], [is_ascending2, …])

Similar to SORT, you should read sort_column as sort_row. The only potentially confusing factor might be the display_ties_mode.

For regular sorting, you can specify it as 0, which is what I have done. For a complete understanding of this argument, please check out this guide: SORTN Tie Modes in Google Sheets – The Four Tiebreakers

Note: I’ve specified TRUE (is_ascending) to sort left to right in ascending order. For descending order, specify FALSE instead.

Resources

You may want to change the order of your data based on different criteria. This tutorial has specifically covered horizontal sorting (sorting columns from left to right) in Google Sheets. Please explore the resources below for a variety of sorting tips.

  1. Sort by Custom Order in Google Sheets [How to Guide]
  2. Formula to Sort By Month Name in Google Sheets
  3. Custom Sort Order in Google Sheets Query [Workaround]
  4. Sort By Sort_Column Name Instead of Sort_Column Header in Google Sheets
  5. Dynamic Sort Column and Sort Order in Google Sheets
  6. How to Properly Sort Alphanumeric Values in Google Sheets
  7. How to Sort by Date of Birth in Google Sheets (Formula Options)
  8. Sort Items by Number of Occurrences in Google Sheets
  9. How to Sort Rows to Bring the Blank Cells on Top in Google Sheets
  10. How to Sort Numbers Formatted as Text in Google Sheets (Formula Approach)
  11. Sort by Day of the Week in Google Sheets
  12. Sort Vertically Merged Cells in Google Sheets (Workaround)
  13. Row-Wise Sorting in a 2-D Array in Google Sheets
  14. How to Custom Sort By Partial Match in Google Sheets
  15. Sorting Data Separated by Line Breaks within Google Sheets Cells
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.

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

2 COMMENTS

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.