Skip to main content
ExcelSORTBYDynamic ArrayData SortingProductivity

1. The Problem: When Traditional Sorting Falls Short

Imagine you're a project manager, staring at a spreadsheet filled with hundreds of tasks. You need to organize these tasks first by their "Status" (e.g., "Overdue," "Blocked," "In Progress," "Completed") and then, for tasks within the same status, by their "Due Date" in ascending order. The standard Excel Sort feature is powerful, but it's a manual process that you have to reapply every time your data changes. You're stuck continually re-sorting, wishing for a dynamic solution that updates automatically.

What is SORTBY? The SORTBY function is an Excel dynamic array function that sorts the contents of a range or array based on the values in a corresponding range or array. It is commonly used to sort data dynamically by one or more columns, allowing for different sort orders (ascending or descending) for each criterion, without altering the original data. This means your sorted list updates automatically as your source data evolves, saving you precious time and ensuring data accuracy. This is exactly the kind of problem the SORTBY function was designed to solve.

2. The Ingredients: Understanding SORTBY's Setup

To truly master the SORTBY function, it's essential to understand its components. Think of these as the fundamental ingredients in your sorting recipe, each playing a crucial role in the final dish. This dynamic array function empowers you to craft custom sort orders with remarkable flexibility and precision.

Here's the exact syntax you'll use:

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

Let's break down each parameter in detail, making sure every ingredient is clearly understood:

Parameter Description
array The range or array you want to sort. This is your entire dataset that you wish to rearrange dynamically.
by_array1 The first array or range by which to sort the array. This could be a single column of your data or a custom calculation.
sort_order1 Optional. Specifies the sort order for by_array1. Use 1 for ascending (this is the default if omitted) or -1 for descending order.
by_array2 Optional. A second array or range to sort by. This allows for multi-level sorting, acting as a tie-breaker for identical values in by_array1.
sort_order2 Optional. Specifies the sort order for by_array2. Again, 1 for ascending (default) or -1 for descending. You can add more by_array and sort_order pairs as needed for complex sorting requirements.

In our experience, understanding how these arguments interact is key to unlocking the full potential of the SORTBY function. You can chain multiple by_array and sort_order pairs to create intricate, multi-level sorting logic, far beyond what simple manual sorting offers, making your data analysis truly dynamic.

3. The Recipe: Step-by-Step Instructions for Dynamic Sorting

Let's put the SORTBY function into action with a real-world scenario. You have a list of project tasks, and you need to sort them first by a custom status order (Overdue, Blocked, In Progress, Completed), and then by Due Date in ascending order. This multi-level, custom sorting is where SORTBY truly shines, giving you a dynamic and self-updating view of your data.

Sample Project Tasks (Original Data in A1:E9):

Task ID Task Name Status Due Date Priority
T001 Design UI/UX In Progress 2024-03-15 High
T002 Develop Backend Blocked 2024-03-20 High
T003 Write Test Cases Completed 2024-03-10 Medium
T004 Review Code In Progress 2024-03-18 High
T005 Deploy Beta Overdue 2024-03-05 Critical
T006 Document Features Completed 2024-03-12 Low
T007 Client Feedback In Progress 2024-03-25 Medium
T008 Bug Fixes Overdue 2024-03-08 High

For our custom status order, let's assume you've listed them in a separate range, say H2:H5: "Overdue", "Blocked", "In Progress", "Completed". This list provides the specific sequence we want for our primary sort.

Here's how to craft your dynamic sorting formula, sorting only the data rows and allowing you to place headers manually or dynamically above the result:

  1. Select Your Output Cell: Click on cell G2. This is where your sorted data will begin to spill. Ensure there's sufficient empty space (8 rows by 5 columns in this case, G2:K9) below and to the right for the entire sorted table to prevent a #SPILL! error.

  2. Define the array (Data Only): We want to sort the task data itself, excluding the header row. So, your array will be A2:E9. This is the core dataset containing all the information we wish to rearrange.

  3. Establish by_array1 for Custom Status Order: To sort by our custom status sequence, we need to convert the text statuses into a numerical order that SORTBY can understand. The MATCH function is ideal here, finding the position of each task's status within our predefined custom order list (H2:H5).

    • The expression MATCH(C2:C9, H2:H5, 0) will return a new array of numbers (e.g., 1 for "Overdue", 2 for "Blocked", etc., assuming H2:H5 contains that order). This array provides the primary sorting key, translating our custom text order into sortable numbers.
  4. Set sort_order1 (Ascending for Custom Order): Since we want tasks with a lower MATCH number (like "Overdue" being 1st) to appear first, we use 1 for ascending order for by_array1.

  5. Establish by_array2 for Due Date: After tasks are sorted by status, we'll sort any tasks sharing the same status by their due date. This refers to the "Due Date" column in your original data.

    • The range D2:D9 is the specific column that will serve as our secondary sort criterion.
  6. Set sort_order2 (Ascending for Due Date): We want the earliest due dates to appear first within each status group, so again, we use 1 for ascending order for by_array2.

  7. Assemble the Full Formula: Combine all these meticulously prepared ingredients into your G2 cell and press Enter.

    =SORTBY(A2:E9, MATCH(C2:C9, H2:H5, 0), 1, D2:D9, 1)
    


    When you press Enter, Excel will dynamically spill the sorted task table into cells `G2:K9`. The tasks will first be grouped according to your custom order: "Overdue," then "Blocked," followed by "In Progress," and finally "Completed." Crucially, within each of these status groups, the tasks will be further sorted by their "Due Date" from earliest to latest. This single **SORTBY** formula provides an incredibly powerful and dynamic view of your project data, always staying current with changes to your source.

**Resulting Sorted Data (Starting in G2, you would typically copy A1:E1 to G1:K1 for headers to complete the view):**

| Task ID | Task Name         | Status      | Due Date   | Priority |
| :------ | :---------------- | :---------- | :--------- | :------- |
| T005    | Deploy Beta       | Overdue     | 2024-03-05 | Critical |
| T008    | Bug Fixes         | Overdue     | 2024-03-08 | High     |
| T002    | Develop Backend   | Blocked     | 2024-03-20 | High     |
| T001    | Design UI/UX      | In Progress | 2024-03-15 | High     |
| T004    | Review Code       | In Progress | 2024-03-18 | High     |
| T007    | Client Feedback   | In Progress | 2024-03-25 | Medium   |
| T003    | Write Test Cases  | Completed   | 2024-03-10 | Medium   |
| T006    | Document Features | Completed   | 2024-03-12 | Low      |

### 4. Pro Tips: Level Up Your Skills with SORTBY

Beyond the basics, there are several ways to elevate your use of the **SORTBY** function and integrate it seamlessly into your workflows. Experienced Excel users often leverage these techniques for maximum efficiency and dynamic reporting.

*   **Best Practice:** Use **SORTBY** when you need to sort a dataset by multiple columns with different sort orders (ascending/descending). This avoids the manual re-sorting overhead and ensures your analysis is always based on the latest, correctly ordered data, making your spreadsheets more robust.

*   **Combine with Other Dynamic Array Functions:** Pair **SORTBY** with functions like `FILTER`, `UNIQUE`, or `XLOOKUP` to create even more powerful, dynamic reports. For instance, you could `FILTER` data for a specific team, then `SORTBY` their tasks by priority and due date, all within a single, self-updating formula. This modularity is a hallmark of modern Excel.

*   **Handle Custom Sort Orders with `MATCH`:** As demonstrated in our recipe, when you need a non-alphabetical or non-numerical sort order for text values (like "Overdue," "Blocked," etc.), create a list of your desired order in a separate range. Then, use `MATCH(your_column, custom_order_range, 0)` as one of your `by_array` arguments. This is a robust and flexible method for advanced sorting requirements that goes beyond simple A-Z or Z-A sorting.

*   **Dynamic Headers:** To include headers with your sorted data dynamically, use `VSTACK` or `HSTACK` to combine your original headers with the `SORTBY` result that only sorts the data rows. For example, `=VSTACK(A1:E1, SORTBY(A2:E9, ...))` would neatly combine your header row with the sorted data below it, creating a fully dynamic table.

### 5. Troubleshooting: Common Errors & Fixes with SORTBY

Even the most seasoned Excel chefs occasionally encounter bumps in the road. When using the **SORTBY** function, two common errors can arise, often stopping a user dead in their tracks. Knowing how to quickly identify and fix them will save you considerable time and frustration, getting you back to analysis faster.

### #VALUE! Error

*   **What it looks like:** Your cell displays `#VALUE!` where your sorted data should be, indicating a problem with the formula's arguments.
*   **Why it happens:** The most common reason for this error is that the 'array' argument and one or more of your 'by_array' arguments have incompatible dimensions. This means they don't have the same number of rows or columns. For example, if your `array` covers `A2:E9` (8 rows, 5 columns), but your `by_array1` is `C2:C10` (9 rows, 1 column), Excel gets confused because it can't correctly match each row of the main array to a corresponding sorting value.
*   **How to fix it:** Carefully check that all your `array` and `by_array` ranges cover the exact same number of rows. If your `array` is `A2:E9`, then `by_array1` should be `C2:C9`, and `by_array2` should be `D2:D9`. Ensure you're selecting only the data rows for sorting, or adjust all ranges consistently if attempting to include headers dynamically.

### #SPILL! Error

*   **What it looks like:** A `#SPILL!` error appears prominently in the cell where you entered your **SORTBY** formula, preventing any output.
*   **Why it happens:** This occurs if the resulting array generated by the **SORTBY** function cannot fit into adjacent cells because there's existing data blocking its path. Dynamic array functions need a clear, contiguous block of empty cells to "spill" their results into. If even one cell in the required output range contains data (even a single space character), you'll encounter this error.
*   **How to fix it:** Identify any data in the cells that the **SORTBY** function needs to fill. For instance, if your sorted result is 8 rows by 5 columns and your formula is in `G2`, check the cells from `G2` to `K9`. Clear the obstructing data by deleting the contents of those cells. Once the spill range is entirely clear, the formula will recalculate and display your sorted data correctly. According to Microsoft documentation, ensuring a clear spill range is crucial for all dynamic array functions.

### 6. Quick Reference: Your SORTBY Cheat Sheet

Keep this handy for when you need a quick reminder of the **SORTBY** function's essentials, ensuring your data is always perfectly ordered:

*   **Syntax:** `SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)`
*   **Most Common Use Case:** Dynamically sorting a table by multiple columns, each with its own specified ascending (`1`) or descending (`-1`) order, without modifying the original data. This is particularly useful for dashboards and reports that require live, sorted views.
*   **Key Gotcha to Avoid:** Mismatched dimensions between your `array` and any `by_array` arguments. Always ensure they cover the same number of rows, or you'll be greeted with a `#VALUE!` error.
*   **Related Functions to Explore:**
    *   `SORT`: Sorts a range or array by columns or rows. Simpler than `SORTBY` as it doesn't allow separate `by_array` arguments for sorting by different columns than what is being returned directly.
    *   `FILTER`: Filters a range of data based on criteria you define. Often used in combination with `SORTBY` to filter and then sort a subset of data.
    *   `UNIQUE`: Returns a list of unique values from a range or array. Can be combined with `SORTBY` to get a sorted list of unique items.
    *   `XLOOKUP`: A modern lookup function that can often replace `VLOOKUP` or `HLOOKUP`. When used with dynamically sorted data, it becomes even more powerful.
👨‍💻

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 💡