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.
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))))
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.
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.
- Sort by Custom Order in Google Sheets [How to Guide]
- Formula to Sort By Month Name in Google Sheets
- Custom Sort Order in Google Sheets Query [Workaround]
- Sort By Sort_Column Name Instead of Sort_Column Header in Google Sheets
- Dynamic Sort Column and Sort Order in Google Sheets
- How to Properly Sort Alphanumeric Values in Google Sheets
- How to Sort by Date of Birth in Google Sheets (Formula Options)
- Sort Items by Number of Occurrences in Google Sheets
- How to Sort Rows to Bring the Blank Cells on Top in Google Sheets
- How to Sort Numbers Formatted as Text in Google Sheets (Formula Approach)
- Sort by Day of the Week in Google Sheets
- Sort Vertically Merged Cells in Google Sheets (Workaround)
- Row-Wise Sorting in a 2-D Array in Google Sheets
- How to Custom Sort By Partial Match in Google Sheets
- Sorting Data Separated by Line Breaks within Google Sheets Cells
Perfect solution!!!
Thanks!
Works like a champ, and updates in realtime. Love it!