Skip to main content
ExcelSORTBY Multiple CriteriaDynamic ArrayData SortingProductivity

The Problem: Taming Unruly Data with Multiple Sorting Needs

Ever stared at a sprawling Excel dataset, feeling a pang of dread as you realize it needs to be sorted—not just once, but by a cascading series of conditions? Perhaps you need to see sales data by region, then by product category, and finally by revenue, all in one coherent view. Manually applying multiple sort levels through the Data tab can be tedious, time-consuming, and prone to errors, especially when your data frequently updates. The traditional sort method is static; any new entries or changes force you to repeat the entire process, disrupting your workflow and eating valuable time.

This common spreadsheet predicament is exactly where the dynamic array SORTBY function shines. What is SORTBY? SORTBY is an Excel function that sorts the contents of a range or array based on the values in one or more corresponding ranges or arrays. It is commonly used to dynamically organize datasets according to multiple specified criteria, eliminating the need for manual sorting and ensuring your data is always presented in the desired order. Without SORTBY, achieving this dynamic, multi-level organization would often involve complex helper columns or iterative manual adjustments, bogging down even the most experienced Excel users. In our experience, struggling with static sorts is a key indicator that a more dynamic solution is needed.

Business Context & Real-World Use Case: Streamlining E-commerce Inventory Management

Imagine you're managing inventory for a rapidly growing e-commerce business. Your raw data comes in as a jumble of product IDs, warehouse locations, stock levels, reorder dates, and product categories. For efficient operations, your logistics team needs to view this inventory sorted first by Warehouse Location (to prioritize deliveries), then by Reorder Date (to address urgent stock needs), and finally by Stock Level (lowest first, to identify items nearing depletion).

Doing this manually is a nightmare. Each time new stock arrives, orders are fulfilled, or a reorder is placed, the entire dataset changes. Relying on the manual "Sort" feature means constantly re-applying three levels of sorting, which is incredibly inefficient. A common mistake we've seen in fast-paced environments is data being sorted incorrectly or not at all, leading to mispicks, delayed shipments, and even lost sales due to perceived out-of-stock items that were actually available but miscategorized. In my years as a supply chain consultant, I've seen teams waste hours on these repetitive tasks, diverting valuable resources from strategic planning.

Automating this sorting process with the SORTBY function provides immediate business value. It ensures your logistics team always sees an up-to-the-minute, perfectly organized view of inventory without any manual intervention. This leads to faster decision-making, optimized warehouse routes, reduced shipping errors, and ultimately, a smoother, more profitable operation. Accurate stock levels and timely reorders directly impact customer satisfaction and the bottom line. Leveraging SORTBY turns a manual chore into a reliable, automated system, giving your business a competitive edge.

The Ingredients: Understanding SORTBY Multiple Criteria's Setup

The SORTBY function is a powerful addition to Excel's dynamic array capabilities, allowing you to sort data using one or more arrays as the sorting criteria. Its exact syntax is deceptively simple, but mastering its parameters unlocks complex sorting logic.

The core syntax for SORTBY is:

=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)

Let's break down each parameter with clear requirements:

Parameter Description Requirements
array This is the range or array of values you want to sort. This will be the output of your SORTBY function. Must be a valid range reference (e.g., A2:D10) or an array of values. It's the entire dataset you want to see rearranged.
by_array1 This is the first range or array by which you want to sort the array. SORTBY will use the values in this range to determine the initial sort order. Must be a range or array with the same number of rows or columns as the array parameter, depending on the orientation of your data. This array dictates the primary sorting logic.
[sort_order1] (Optional) Specifies the sort order for by_array1. 1 for ascending (default), -1 for descending. Can be omitted for ascending order (Excel defaults to 1). If provided, it must be either 1 or -1. Any other numerical value will result in an error.
[by_array2] (Optional) This is the second range or array by which you want to sort, applied only to rows that have identical values in by_array1. This enables multi-level sorting with the SORTBY function. Similar to by_array1, it must be a range or array compatible in size with the array. This array provides the secondary sorting logic, breaking ties from by_array1. You can specify up to 126 pairs of by_array and sort_order arguments.
[sort_order2] (Optional) Specifies the sort order for by_array2. 1 for ascending (default), -1 for descending. Similar to sort_order1, it must be 1 or -1. This controls the order for the secondary sort. Each by_array argument needs a corresponding sort_order if you want to specify it; otherwise, it defaults to ascending.

Understanding these parameters is the core of mastering the SORTBY function. The flexibility to add multiple by_array and sort_order pairs makes SORTBY incredibly powerful for complex data organization.

The Recipe: Step-by-Step Instructions for Multi-Criteria Sorting

Let's put the SORTBY function into action with a practical example. Imagine you have a dataset of sales transactions and you want to sort them: first by Sales Region in ascending order, then by Product Category in ascending order, and finally by Sales Amount in descending order to quickly identify top performers within each category and region.

Here's our sample sales data:

Region Product Category Sales Rep Sales Amount Transaction Date
North Electronics Alice M. $1,200 2023-01-15
South Apparel Bob D. $800 2023-01-18
East Electronics Carol S. $1,500 2023-01-10
West Home Goods David L. $750 2023-01-22
North Home Goods Alice M. $950 2023-01-16
East Apparel Frank P. $600 2023-01-12
South Electronics Bob D. $1,100 2023-01-19
West Apparel Gina H. $700 2023-01-23
North Electronics Alice M. $1,300 2023-01-17
East Home Goods Carol S. $1,000 2023-01-11

Assume this data resides in cells A2:E11, with headers in A1:E1.

Here's how to build your SORTBY formula:

  1. Select Your Output Cell: Click on cell G2 (or any empty cell where you want the sorted results to spill). This is where the SORTBY function will begin to display your organized data.

  2. Identify the Array to Sort: Your entire dataset, including headers, is A1:E11. If you want to sort just the data rows, select A2:E11. For this example, let's sort the entire table, including headers, for clarity in the output. So, your array will be A1:E11.

  3. Specify the First Sorting Criterion (by_array1): You want to sort by Region in ascending order. The Region column is A1:A11. Type this as the second argument.

  4. Define the First Sort Order (sort_order1): For ascending order, the value is 1.

  5. Specify the Second Sorting Criterion (by_array2): Next, sort by Product Category in ascending order. The Product Category column is B1:B11. Add this as the fourth argument.

  6. Define the Second Sort Order (sort_order2): For ascending order, the value is 1.

  7. Specify the Third Sorting Criterion (by_array3): Finally, sort by Sales Amount in descending order. The Sales Amount column is D1:D11. Add this as the sixth argument.

  8. Define the Third Sort Order (sort_order3): For descending order, the value is -1.

Combining these steps, your final SORTBY formula in cell G2 will be:

=SORTBY(A1:E11, A1:A11, 1, B1:B11, 1, D1:D11, -1)

Press Enter, and watch as your entire dataset instantly spills into cells G2:K11, perfectly sorted according to your three criteria. The SORTBY function has efficiently organized your data.

Here's what the sorted output will look like (in G2:K11):

Region Product Category Sales Rep Sales Amount Transaction Date
East Apparel Frank P. $600 2023-01-12
East Electronics Carol S. $1,500 2023-01-10
East Home Goods Carol S. $1,000 2023-01-11
North Electronics Alice M. $1,300 2023-01-17
North Electronics Alice M. $1,200 2023-01-15
North Home Goods Alice M. $950 2023-01-16
South Apparel Bob D. $800 2023-01-18
South Electronics Bob D. $1,100 2023-01-19
West Apparel Gina H. $700 2023-01-23
West Home Goods David L. $750 2023-01-22

Notice how the table is first sorted by Region (East, North, South, West). Within "East," it's sorted by Product Category (Apparel, Electronics, Home Goods), and then for "East - Electronics," the $1,500 sale comes before the $1,000 sale if there were more "East - Electronics" entries, due to the descending Sales Amount sort. This dynamic array behavior of SORTBY means if your original data in A1:E11 changes, your sorted output in G2:K11 will update automatically!

Pro Tips: Level Up Your Skills with SORTBY

The SORTBY function is powerful on its own, but a few expert tips can elevate your data manipulation game.

  • Evaluate data thoroughly before deployment. Before applying SORTBY to critical datasets, always verify the integrity and consistency of your source data. Incorrect data types (e.g., numbers stored as text) or hidden characters can lead to unexpected sorting results, even with a perfectly constructed SORTBY formula. A quick check of column formats can save significant troubleshooting time later.
  • Combine with FILTER and UNIQUE: For more advanced scenarios, nest SORTBY within or around other dynamic array functions. For instance, you could FILTER your data first for a specific region, then use SORTBY to organize the filtered results. Or, combine UNIQUE with SORTBY to get a sorted list of unique values based on multiple criteria. This synergistic approach unleashes even greater analytical power, allowing you to slice, dice, and organize data in complex ways.
  • Utilize Named Ranges: Instead of using cell references like A1:E11, consider defining Named Ranges for your data and sorting arrays (e.g., SalesData, SalesRegion, ProductCategory). This makes your SORTBY formulas much more readable, easier to audit, and less prone to errors if columns are inserted or deleted in your source data. Experienced Excel users prefer this method for its robustness and clarity.
  • Handling Blanks: Be aware that SORTBY treats blank cells differently depending on the data type. Text blanks typically sort after non-blanks in ascending order, while numeric blanks are treated as zeros. If blanks are present in your sorting criteria, test the SORTBY behavior to ensure it aligns with your expected outcome.

Troubleshooting: Common Errors & Fixes for SORTBY

Even the most seasoned Excel users can encounter hiccups. When your SORTBY function doesn't behave as expected, it's often due to a few common culprits. Here's how to diagnose and fix them.

1. #NAME? Error

  • What it looks like: Your cell displays #NAME?
  • Why it happens: This error is almost always due to formula syntax typos. Excel doesn't recognize the function name or one of its arguments. You might have misspelled SORTBY, used incorrect punctuation, or forgotten a comma. This is a very common error, especially when typing longer formulas with multiple by_array and sort_order arguments for the SORTBY function.
  • How to fix it:
    1. Check Spelling: Carefully review the function name. Is it SORTBY and not SORTBYY or SORT_BY?
    2. Verify Commas & Parentheses: Ensure all arguments are separated by commas and that every opening parenthesis has a corresponding closing one. Count them!
    3. Argument Types: Confirm that range references are valid (e.g., A1:A10 not A1;A10). The sort_order arguments must be 1 or -1.

2. #VALUE! Error

  • What it looks like: Your cell displays #VALUE!
  • Why it happens: The #VALUE! error often indicates a problem with the dimensions of your arrays. Specifically, by_array arguments must have the same number of rows (or columns) as your main array argument. For example, if your array covers rows 2 to 10 (A2:E10), but your by_array1 covers rows 1 to 10 (A1:A10), SORTBY will return #VALUE!. This mismatch confuses the SORTBY function, as it doesn't know how to align the sorting criteria with the data.
  • How to fix it:
    1. Match Array Dimensions: Go through each by_array argument (by_array1, by_array2, etc.) and ensure their row (or column) count exactly matches the array argument. If your data is in A2:E10, all by_array arguments should also refer to ranges of 9 rows (e.g., A2:A10, B2:B10).
    2. Check for Non-Numeric sort_order: Although less common for #VALUE! in SORTBY, ensure that your sort_order arguments are strictly 1 or -1. Any other text or non-numeric value here could trigger an error.

3. Unexpected Sort Order / Incorrect Results

  • What it looks like: The SORTBY function executes without an error, but the data isn't sorted in the way you intended. Rows might appear in a seemingly random order, or one of your sorting levels isn't being applied correctly.
  • Why it happens: This isn't an "error" in Excel's eyes, but rather a logical discrepancy between your expectation and the formula's instruction. Common causes include:
    • Incorrect sort_order: You intended ascending (1) but typed descending (-1), or vice-versa.
    • Mixed Data Types: A column intended for numeric sort contains numbers stored as text (e.g., '123' instead of 123). Excel will sort these differently than true numbers.
    • Order of by_array arguments: The hierarchy of your sorting criteria matters. by_array1 is primary, by_array2 is secondary (breaking ties from by_array1), and so on. If you've reversed the order, the SORTBY function will produce a different result.
    • Hidden Characters/Trailing Spaces: Text entries can look identical but sort differently if one has a trailing space or a non-printable character.
  • How to fix it:
    1. Verify sort_order Arguments: Double-check each 1 and -1 in your SORTBY formula to ensure they reflect the desired ascending or descending order for each criterion.
    2. Inspect Data Types: Use functions like ISTEXT() or ISNUMBER() on your sorting columns to identify mixed data types. Convert text-numbers to actual numbers if necessary (e.g., using VALUE() or Text to Columns).
    3. Re-evaluate Sorting Hierarchy: Read your SORTBY formula from left to right (after the main array). Is by_array1 truly your primary sort? Is by_array2 the correct tie-breaker? Adjust the order of the by_array and sort_order pairs if needed.
    4. Clean Your Data: For text columns, use TRIM() to remove leading/trailing spaces. CLEAN() can remove non-printable characters that might affect sorting. According to Microsoft documentation, clean data is crucial for predictable outcomes with SORTBY.

Quick Reference

Element Description
Syntax =SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
Common Use Case Dynamically sorting a dataset by multiple columns (e.g., Region, then Product, then Sales Amount) without manual intervention.

Related Functions

👨‍💻

Written by The Head Chef

Former 10-year Financial Analyst who survived countless month-end closes. I build these recipes to save you from weekend-ruining spreadsheet errors.

Read the full story →

You might also find these useful 💡