Sort Each Row Individually in Excel Using a LAMBDA Formula

Published on

Sorting rows in Excel typically refers to rearranging entire datasets based on values in a specific column or row. But what if you want to sort each row individually in Excel, from left to right? While Excel doesn’t offer a built-in feature for this, the power of LAMBDA functions and dynamic arrays makes it possible to perform this operation with a single formula.

In this tutorial, you’ll learn how to sort each row individually in Excel—whether in ascending or descending order—using a flexible LAMBDA-based formula. This technique is especially useful when dealing with row-wise scores, timelines, or customized data transformations, and it requires no helper columns or manual sorting.

Formula to Sort Each Row Individually in Excel

=LET(
   range, array, 
   DROP(
      REDUCE("", SEQUENCE(ROWS(range)), LAMBDA(acc, seq, VSTACK(acc, SORT(CHOOSEROWS(range, seq), 1, -1, TRUE)))), 1
   )
)

Replace array with your actual data range.

Example Dataset

Here’s a real-life dataset where sorting each row individually in Excel can be helpful—for example, analyzing monthly production figures for different fruits, highlighting each fruit’s production from highest to lowest:

ProductJanFebMarAprMay
Apple12095130110125
Mango8510510011590
Blue Berry140135120125150

The following formula sorts each product’s monthly production values in descending order:

=LET(
   range, B2:F4, 
   DROP(
      REDUCE("", SEQUENCE(ROWS(range)), LAMBDA(acc, seq, VSTACK(acc, SORT(CHOOSEROWS(range, seq), 1, -1, TRUE)))), 1
   )
)

To include product names in the result, use the HSTACK function:

=HSTACK(A2:A4, <above_formula>)
Example showing how to sort each row individually in Excel using a LAMBDA formula

Formula Explanation

  • SORT(CHOOSEROWS(range, seq), 1, -1, TRUE) sorts one row (specified by seq) from left to right in descending order.
    • CHOOSEROWS(range, seq) extracts the seq-th row from the data range.
  • VSTACK(acc, ...) appends the sorted row vertically to the result accumulator.
  • REDUCE("", SEQUENCE(ROWS(range)), LAMBDA(acc, seq, ...)) iterates through each row index and builds the final sorted array.
  • DROP(..., 1) removes the initial empty element introduced by REDUCE.

This approach lets you sort each row individually in Excel without manual intervention.

Frequently Asked Questions

Can I sort rows horizontally in Excel using a formula?
Yes. While Excel doesn’t have a built-in command for this, you can use a combination of LAMBDA, SORT, and dynamic array functions to sort each row individually from left to right. For a single row, the SORT function with its last argument set to TRUE can sort across columns. (Syntax: SORT(array, [sort_index], [sort_order], [by_col]))

Which Excel versions support sorting each row with this formula?
This method requires Excel 365 or Excel 2021+, which support LAMBDA, SORT, REDUCE, SEQUENCE, and other dynamic array functions.

Can I sort each row in ascending order instead?
Yes. Change the sort_order from -1 to 1 in the formula to sort from lowest to highest.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

Design Logic Behind the Perpetual Calendar Heatmap in Excel

This post is a focused deep dive into the design logic behind an Excel...

Perpetual Calendar Heatmap in Excel (Fully Dynamic, True Calendar)

Excel doesn’t have a native calendar heatmap feature. When you try to visualize daily...

Why Most Reverse Running Total Formulas in Excel Break with Negative Values

Excel users often rely on the SCAN function to calculate running totals. While SCAN...

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.