Function Problems

How to Sort Data Horizontally and Vertically with SORTBY Function in Spreadsheets

Phi Hung VoPhi Hung Vo Nov 11, 2026 869 views

Question details

The user is inquiring whether the SORTBY function can be used to organize data horizontally from left to right, in addition to standard vertical sorting.

How to Sort Horizontally and Vertically Using the SORTBY Function
Product
WPS Spreadsheet
Device & OS
not provided
Scenario
Sorting a dataset where the primary sorting criteria is laid out in a row rather than a column, requiring a horizontal reordering of the columns.
Observed behavior
The user needs clarification on how the orientation of the 'by_array' argument dictates the sorting direction to successfully execute a horizontal sort.
Before you start

Ensure that the source array and your sorting criteria array (the 'by_array') have the exact same dimensions in the direction you are sorting to prevent a #VALUE! error.

Solution 1Recommended

Sort Data Horizontally Across Columns with SORTBY

By providing a horizontal range as your sort criteria, the SORTBY function will automatically recognize the array shape and sort your data from left to right.

The sorting direction of SORTBY is entirely dependent on the shape of the array you supply in the formula. If your sorting criteria is horizontal (e.g., a single row spanning multiple columns), the output will be sorted horizontally.

1
Identify the data ranges

Determine the horizontal range of data you want to return (e.g., B1:E1) and the corresponding horizontal row containing the values you want to sort by (e.g., B2:E2).

2
Select the destination cell

Click on an empty cell where you want the leftmost value of your sorted horizontal array to begin.

3
Enter the SORTBY formula

Type `=SORTBY(B1:E1, B2:E2, 1)` into the formula bar. The '1' signifies an ascending sort order. You can change it to '-1' for descending order.

4
Execute the function

Press Enter. The results will dynamically spill horizontally across the adjacent columns in the specified sorted order.

Sort Data Horizontally Across Columns with SORTBY
Spill Range Requirement: Make sure there are enough empty cells to the right of your formula cell; otherwise, you will encounter a #SPILL! error.
Advanced Spreadsheet Tools

Easily Organize Data with Dynamic Arrays in WPS Office

WPS Spreadsheet fully supports powerful dynamic array formulas, including the SORTBY function. You can seamlessly sort complex datasets across columns or rows without relying on complicated macros or manual data shifting.

  1. 1. Open your spreadsheet: Launch WPS Spreadsheet and open the document containing the data you wish to sort.
  2. 2. Select a blank output area: Click on a blank cell ensuring there is sufficient room either below (for vertical sorts) or to the right (for horizontal sorts).
  3. 3. Write the SORTBY formula: Type `=SORTBY(` and highlight your source data array. Type a comma, then highlight your sorting array (row or column), followed by your sort order.
  4. 4. Generate the results: Press Enter to instantly populate the dynamic array, displaying your correctly sorted data.
Seamless support for dynamic array functions like SORT, SORTBY, and FILTER.100% compatible with Microsoft Excel (.xlsx) formats and syntax.Lightweight architecture ensures fast calculation of large horizontal and vertical arrays.Clean, intuitive interface for effortless formula building and data analysis.
microsoft office alternative - wps office

Frequently Asked Questions

What causes a #VALUE! error when using the SORTBY function?

A #VALUE! error typically occurs if the array you want to return and the array you are sorting by (the 'by_array') do not match in size. For a horizontal sort, ensure both the return array and the sort array span the exact same number of columns.

Can I sort horizontally by multiple rows using SORTBY?

Yes, you can add additional horizontal arrays to act as secondary sorting levels. For example, `=SORTBY(B1:E1, B2:E2, 1, B3:E3, -1)` will sort horizontally first by row 2 in ascending order, and then resolve ties by sorting row 3 in descending order.

What is the difference between the SORT and SORTBY functions?

The SORT function sorts data based on an index number corresponding to a column or row within the selected source array itself. The SORTBY function, however, allows you to sort an array based on a completely separate range or array that does not necessarily need to be part of the final output.

Will my SORTBY results update automatically if the original data changes?

Yes. Because SORTBY is a dynamic array function, any modifications made to the values in your source range or your sorting criteria array will immediately reflect in the spilled output cells without needing to refresh.