SORTBY function

SORTBY reorders an array based on one or more sort columns, returning rows in the order specified by your criteria—perfect when you need multi-column sorting.

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

Generate a SORTBY formula

Describe what you need. The generator will reach for SORTBY where SORTBY is the right tool, and tell you when it is not.

How to get a better answer
  • Name your columns by letter and by header: "column F (Net Value)" beats "the amount column".
  • State every condition, including the negatives — "not cancelled" changes the formula's shape.
  • Say where the data starts if it is not row 1, and whether it will grow.
  • Check the settings above match your spreadsheet: the wrong argument separator is a syntax error on your machine.

Arguments

How SORTBY reads its arguments
arrayrequiredby_array1requiredsort_order1optionalSORTBY
ArgumentRequiredDescription
arrayRequiredThe data range or array to sort; rows will be reordered without any values being modified.
by_array1RequiredThe reference array whose values determine the sort order; must have the same number of rows as array.
sort_order1Optional1 for ascending order (default) or -1 for descending; omit to default to ascending (1).
...RepeatingOptionally repeat by_array and sort_order pairs to sort by multiple criteria in order of priority.

Returns

An array with the same rows as the input array, but reordered according to the sort criteria.

Availability

Excel: 365 / 2021+ · Google Sheets: Supported

Worked examples

1. Rank high-priority tickets by customer satisfaction

TicketIDPriorityOpenedClosedAgentCSAT
T001High9/19/2Sarah5
T002Low9/19/5Mike4
T003Medium9/29/3Sarah3
T004High9/39/4Lisa5
T005Low9/49/8Mike2
T006Medium9/59/6Sarah4
=SORTBY(A1:F6, B1:B6, -1, F1:F6, -1)

Result: T001High9/19/2Sarah5T004High9/39/4Lisa5T006Medium9/59/6Sarah4T003Medium9/29/3Sarah3T002Low9/19/5Mike4T005Low9/49/8Mike2

This sorts all tickets by priority level first (High before Medium before Low), then by CSAT score within each priority. High-priority tickets with the best customer satisfaction appear at the top, making it easy to identify your most satisfied urgent cases.

2. Group tickets by agent and rank their CSAT scores

TicketIDPriorityOpenedClosedAgentCSAT
T001High9/19/2Sarah5
T002Low9/19/5Mike4
T003Medium9/29/3Sarah3
T004High9/39/4Lisa5
T005Low9/49/8Mike2
T006Medium9/59/6Sarah4
=SORTBY(A1:F6, E1:E6, 1, F1:F6, -1)

Result: T004High9/39/4Lisa5T002Low9/19/5Mike4T005Low9/49/8Mike2T001High9/19/2Sarah5T006Medium9/59/6Sarah4T003Medium9/29/3Sarah3

This groups tickets by agent name alphabetically, then ranks each agent's cases by satisfaction score (highest first). It reveals which agents are delivering excellent customer experiences and where performance improvement might be needed.

3. Sort tickets by fastest resolution time

TicketIDPriorityOpenedClosedAgentCSAT
T001High9/19/2Sarah5
T002Low9/19/5Mike4
T003Medium9/29/3Sarah3
T004High9/39/4Lisa5
T005Low9/49/8Mike2
T006Medium9/59/6Sarah4
=SORTBY(A1:F6, D1:D6-C1:C6, 1)

Result: T001High9/19/2Sarah5T003Medium9/29/3Sarah3T004High9/39/4Lisa5T006Medium9/59/6Sarah4T002Low9/19/5Mike4T005Low9/49/8Mike2

SORTBY accepts a calculated array (Closed minus Opened) as the sort key, showing resolution time in days. All single-day resolutions appear first, followed by longer resolution times, making it easy to spot which cases were handled fastest.

Common errors

Which SORTBY error are you seeing?
SORTBY returned an error#VALUE!
Verify that your sort key array spans the same rows as your data. For example, if sorting A1:F6, your by_array must also reference rows 1–6 (e.g., B1:B6 or E1:E6).
#SPILL!
Clear or move any content in the target range, or place the SORTBY formula in an empty area where the full sorted array can expand without obstruction.
#N/A
Check your sort column for error values and replace them with valid data, or use IFERROR to substitute a placeholder value before passing the array to SORTBY.
ErrorWhy it happensHow to fix it
#VALUE!The by_array has a different number of rows than the main array being sorted.Verify that your sort key array spans the same rows as your data. For example, if sorting A1:F6, your by_array must also reference rows 1–6 (e.g., B1:B6 or E1:E6).
#SPILL!The sorted result wants to spill into cells that already contain data or formulas.Clear or move any content in the target range, or place the SORTBY formula in an empty area where the full sorted array can expand without obstruction.
#N/AThe by_array contains an error value (such as #REF! or #DIV/0!) or a broken reference.Check your sort column for error values and replace them with valid data, or use IFERROR to substitute a placeholder value before passing the array to SORTBY.

Tips and when to use something else

  • SORTBY can accept calculated arrays as sort keys—e.g., D1:D6-C1:C6 for date differences—without needing a helper column.
  • Each sort_order argument applies only to the by_array immediately before it; you can mix ascending and descending in a single formula.
  • If you only need to sort by one column and don't need multi-column logic, use SORT instead—it's simpler and often faster.
  • SORTBY returns entire rows; to keep only certain columns after sorting, nest it inside CHOOSECOLS or select columns from the result.

Frequently asked questions

How do I sort in descending order with SORTBY?
Pass -1 as the sort_order argument. For example, =SORTBY(A1:F6, F1:F6, -1) sorts by column F in descending order. You can mix ascending and descending by using different sort_order values for each by_array.
Can SORTBY sort by multiple columns at once?
Yes—provide additional by_array and sort_order pairs. For example, =SORTBY(A1:F6, B1:B6, -1, F1:F6, -1) sorts by Priority (descending) first, then by CSAT (descending) within each priority level.
Does SORTBY change the original data?
No. SORTBY returns a new sorted array and does not modify your source data. You can place the formula result anywhere without affecting the original table.
Can I sort by a formula or calculated value?
Yes. SORTBY accepts formulas in the by_array argument—for example, =SORTBY(A1:F6, D1:D6-C1:C6, 1) sorts by the difference between two date columns.

Need a different formula?

The full generator is not scoped to one function — describe any spreadsheet problem and it will pick.

Open the formula generator

Reviewed 2026-09-17