WorkbookLogic
FunctionDynamic ArrayadvancedSORTBYIFANDDATE

SORTBY Function

By WorkbookLogic

The Excel SORTBY function returns a dynamically sorted array using one or more aligned key ranges, including keys omitted from the output.

ExcelLast reviewed Sep 11, 2026
sortby-function-v1.0.0.xlsx
Excel SORTBY Function
SORTBY reorders an array using aligned sort keys, including keys omitted from the output.
Source data, editableDynamic sorted views
NameRegionScoreDateExampleFormula syntaxNameRegionScoreDate
JordanEast78Aug 18, 2026Full table by date=SORTBY(A12:D17,D12:D17,1)CaseyNorth88Aug 10, 2026
AveryNorth92Aug 12, 2026DevonSouth88Aug 11, 2026
MorganSouth97Aug 20, 2026AveryNorth92Aug 12, 2026
CaseyNorth88Aug 10, 2026RileyEast85Aug 15, 2026
RileyEast85Aug 15, 2026JordanEast78Aug 18, 2026
DevonSouth88Aug 11, 2026MorganSouth97Aug 20, 2026
Names by hidden score=SORTBY(A12:A17,C12:C17,-1)Morgan
Avery
Casey
Devon
Riley
Jordan
Reliability checks
Date-sorted table begins with Casey on 10-Aug-2026OK
Hidden-score sort begins with MorganOK

Overview

The Excel SORTBY function reorders an array according to one or more separate sort-key arrays. A key may be excluded from the returned output, and additional key/order pairs can resolve ties. The result spills dynamically and leaves the source data unchanged.

Quick reference

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

Returns: A dynamic array containing array reordered by the supplied sort keys. (array)

Arguments:

  • array (required) — The range or array returned in sorted order.
  • by_array1 (required) — The first aligned row or column used as a sort key.
  • sort_order1 (optional) — 1 for ascending or -1 for descending; default is ascending.
  • by_array2, sort_order2 (optional) — An additional aligned sort key and its order; more pairs may follow.

The source array and first aligned sort-key array are required. Sort order and additional key/order pairs are optional; omitted order defaults to ascending.

Common uses

  • Sort a visible name list by an omitted score or priority.
  • Order full records by date.
  • Apply region, score, and name tie-breakers.
  • Sort by a calculated helper range without displaying the helper.

Calculations

SORTBY pairs each row or column in array with the corresponding value in by_array1, orders those keys, and returns array in the resulting sequence. The key range must align with the dimension being sorted, but it does not need to be part of the returned columns.

Additional key/order pairs are evaluated in priority order. The second key resolves ties in the first, the third resolves remaining ties, and so on. Each direction uses 1 for ascending or -1 for descending.

The formula creates a dynamic spilled view rather than an in-place sort. Existing content in the required output area causes #SPILL!.

Function behavior

Situation

Behavior

Hidden sort key

Supported; by_array need not be returned in array.

Multiple keys

Additional key/order pairs resolve ties in sequence.

Ascending order

Use 1, the default.

Descending order

Use -1.

Key alignment

Each by_array must align with the sorted dimension.

Source changes

Recalculates and reorders the spilled result.

Occupied spill area

Returns #SPILL!.

Common errors

Misaligned sort-key range

What happens:
The key cannot be paired with each source row or column, so the formula errors.

Better approach:
Use key arrays with the same row or column count as array.

Omitting an intended direction

What happens:
That key uses the default ascending order.

Better approach:
Provide an explicit 1 or -1 for every key where direction matters.

Expecting an in-place sort

What happens:
The source order does not change.

Better approach:
Treat SORTBY as a formula-driven view and keep its spill area clear.

Example

Example 1 — Sort a Full Table by Date

=SORTBY(A12:D17,D12:D17,1)

On SORTBY Example, the table is returned in ascending date order. Casey on 10-Aug-2026 appears first and Morgan on 20-Aug-2026 appears last.

Example 2 — Sort Names by an Omitted Score Key

=SORTBY(A12:A17,C12:C17,-1)

Only names are returned, but the score range controls descending order. Morgan, with score 97, appears first.

Example 3 — Multiple Sort Levels

=SORTBY(A12:D17,B12:B17,1,C12:C17,-1)

This pattern groups regions alphabetically and then puts the highest score first inside each region.

Example workbook

The workbook contains SORTBY Example and Notes. The main sheet provides six editable synthetic records, a date-sorted full-table result, and a names-only result sorted by a hidden score key. IF, AND, and DATE checks confirm the first result in each spill; both display OK.

Key takeaway

Use SORTBY when the sort key should be referenced directly, omitted from the output, or combined with additional tie-breaker keys.