HomeGoogle DocsSpreadsheetRow-Wise Sorting in a 2-D Array in Google Sheets

Row-Wise Sorting in a 2-D Array in Google Sheets

Published on

Row-wise sorting is the process of sorting each row in a 2-D array using an array formula in Google Sheets. We can do this using the BYROW or REDUCE functions with the SORT function. The QUERY function can also be used, but it is not recommended because it can be more complex for some users.

Here is an example of row-wise sorting a 2-D array in Google Sheets:

Sample Data (in cell range B2:E5)

The source data is in the range B2:E5, and the result is in the range B9:E12.

We can easily handle this using a non-array formula (a copy-paste or drag-down formula) in Google Sheets. However, I would rather use an array formula since the source data may not always be physical. It can also be an expression such as the output of the QUERY or some other functions.

However, for those of you who would like to see the non-array formula for sorting each row individually in a 2D array, here it is:

=TRANSPOSE(SORT(TRANSPOSE(B2:E2),1,1))

Note: To sort rows in descending order replace 1 with 0.

Insert the above formula in cell B9 and copy-paste it into B10:B12. Alternatively, you can drag the B9 fill handle (a small blue square in the bottom right corner) down.

Row-Wise Sorting: Non-Array Formula

Related: How to Sort Horizontally in Google Sheets.

We will convert this into an array formula, and I will explain the role of TRANSPOSE in the process.

Row-Wise Sorting in a 2-D Array: BYROW

Let’s start by using BYROW, which is my recommended solution to row-wise sorting of a 2D array in Google Sheets.

In the above example, we have a 2D array in the range B2:E5 and can sort each row of this array in ascending order by using the following array formula in cell B9.

=BYROW(B2:E5,LAMBDA(row,TRANSPOSE(SORT(TRANSPOSE(row),1,1))))
Row-Wise Sorting: Non-Array Formula

You are not restricted to entering the formula in cell B9. The thumb rule is to enter the formula in any cell and give it enough room (blank cells) to expand down and to the right.

If you want row-wise sorting in descending order, use this one.

=BYROW(B2:E5,LAMBDA(row,TRANSPOSE(SORT(TRANSPOSE(row),1,0))))

The difference is negligible, which you can see from the formulas themselves.

How does this formula work?

BYROW Syntax:

BYROW(array_or_range, lambda)

The BYROW function applies a lambda to each row in the array_or_range and returns an array of the results.

  • array_or_range: B2:E5 (it will be separated by rows such as B2:E2, B3:E3, B4:E4, and B5:E5).
  • lambda: Please refer to the syntax below.

LAMBDA Syntax: LAMBDA([name, …], formula_expression)

  • name: row (which represents the row in the array_or_range).
  • formula_expression: TRANSPOSE(SORT(TRANSPOSE(row),1,1))

This formula_expression first TRANSPOSEs the row, then SORTs the transposed row, and then TRANSPOSEs the sorted row again. This applies to each row in the array_or_range.

Additional Tip

We can also use the TOCOL and TOROW functions instead of the TRANSPOSE function in the formula_expression part. Here is the formula expression:

TOROW(SORT(TOCOL(row), 1, 1))

This formula expression first TOCOLs the row, then SORTs the TOCOLed row and then TOROWs the sorted row again.

The TOCOL function converts a range of cells into a single column, while the TOROW function converts a range of cells into a single row.

By using the TOCOL and TOROW functions, we can avoid having to use the TRANSPOSE function twice. This can make the formula more concise and easier to understand.

I hope the above explanation has helped you understand how to use the BYROW function for row-wise sorting in a 2D array in Google Sheets.

Row-Wise Sorting in a 2-D Array: REDUCE

Though I prefer the above BYROW approach for row-wise sorting of a 2D array, you can also use the REDUCE function for the same.

Note: This is just to help you learn more about the advanced use of REDUCE in Google Sheets.

Formula:

=REDUCE(,SEQUENCE(ROWS(B2:E5)),LAMBDA(a,v,IFNA(VSTACK(a,TOROW(SORT(TOCOL(CHOOSEROWS(B2:E5,v))))))))

It has two drawbacks, not in terms of performance, but in terms of coding and output.

  • It is more complex to code than the BYROW approach.
  • It returns an empty row at the top of the result.

Can you explain how this formula performs row-wise sorting in Google Sheets?

Yes. Here is the structure of the formula.

REDUCE Syntax:

 REDUCE(initial_value, array_or_range, lambda)

intial_value: null

array_or_range: SEQUENCE(ROWS(B2:E5)) (serial numbers 1 to 4 represent the rows in the range B2:E5).

lambda: Please see the syntax below.

LAMBDA Syntax: LAMBDA([name1, name2], formula_expression)

  • name1: a which represents the accumulator (the accumulated value in each step).
  • name2: v which represents the row in the array_or_range.
  • formula_expression: IFNA(VSTACK(a,TOROW(SORT(TOCOL(CHOOSEROWS(B2:E5,v))))))

The VSTACK function vertically stacks the accumulator (which is initially null) with the following: TOROW(SORT(TOCOL(CHOOSEROWS(B2:E5,v))))

What does it do?

The CHOOSEROWS function in the formula expression returns the first row of the array. The TOCOL function converts the row to a column. The SORT function sorts the column. The TOROW function transforms the sorted column back into a row.

The array is passed to the accumulator.

This process repeats for each row in the array. The final accumulator value, which is returned by the formula, is a row-wise sorted 2D array.

To put it simply, the formula takes the first row of the array, sorts it, and then adds it to the accumulator. This process repeats for each row in the array, and the final accumulator value is a row-wise sorted 2D array.

That’s all. Thanks for the stay. Enjoy!

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.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to 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.