formulastreak

Dynamic arrays function

SORTBY function in Excel

SORTBY sorts a range by the values in another range, so you can sort names by sales without moving columns.

Checked against Microsoft's and Google's documentation on · Example calculated on a live sheet

Syntax

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

Arguments in [brackets] are optional. Google Sheets doesn't have this function.

Example

=SORTBY(A2:A9,C2:C9,-1) reads: Return the reps A2:A9 sorted by their July sales C2:C9, highest first.

Q3 sales team · edit H2 and watch it recalculate
Click a cell (or use the arrow keys) to insert its address · Enter shows the resultH2 = Lina Chen
ABCDEFGH
1RepRegionJulAugSepTargetBy July
2Amara OseiNorth$12,400$13,100$11,800$36,000Lina Chen
3Ben FischerSouth$9,800$10,250$12,100$30,000Hugo Martin
4Lina ChenEast$15,200$14,800$16,900$45,000Farid Haddad
5Diego AlvarezWest$8,700$9,100$8,400$28,000Amara Osei
6Eva NovakNorth$11,200$12,600$13,900$36,000Eva Novak
7Farid HaddadSouth$13,300$12,900$14,100$39,000Grace Kim
8Grace KimEast$10,100$9,600$10,800$33,000Ben Fischer
9Hugo MartinWest$14,500$15,800$15,100$42,000Diego Alvarez

H2 shows Lina Chen, the best July, down to Diego Alvarez in H9.

How the spreadsheet calculates it

Sorts A2:A9 by the values in C2:C9, largest first, and spills the result.

  1. 1. SORTBY: Sorts A2:A9 by the values in C2:C9, largest first, and spills the result.
    • array = `A2:A9`: what to return
    • by_range = `C2:C9`: what to sort by
    • order = `-1`: `1` ascending, `-1` descending

More SORTBY examples

  • =SORTBY(A2:A9,C2:C9,-1)Reps by July sales, highest first.
  • =SORTBY(A2:B9,B2:B9,1,C2:C9,-1)By region, then by July sales within each region.
  • =TAKE(SORTBY(A2:A9,C2:C9,-1),3)The top three (Excel 2024 or Microsoft 365).

Paste any of these into the formula explainer to see it step by step.

Tips

  • Make every by_array the same height as array.
  • Use a second sort key to break ties in a consistent order.
  • Refer to the sorted result with H2# so other formulas follow it as it grows or shrinks.
  • Sort by a calculation by giving it as by_array, for example a total of two columns added together.

What SORTBY is used for

Leaderboards and rankings: reps by sales, products by revenue, tasks by due date, without changing the order of the source data.

Arguments

ArgumentRequiredWhat it is
arrayYesThe range to return, sorted.
by_array1YesThe range to sort by, as tall as array.
sort_order1No1 ascending (the default) or -1 descending.
by_array2, sort_order2NoA second sort key for ties.

Common mistakes

  • Using SORTBY in Google Sheets: it doesn't have it. Write SORT(A2:A9,C2:C9,FALSE).

  • Using it in a file shared with Google Sheets, which doesn't have SORTBY.

  • A by_array of a different height from array.

In Google Sheets

Google Sheets doesn't have SORTBY. Its SORT does the same job, because its sort column can be a range outside the data: SORT(A2:A9, C2:C9, FALSE).

Errors you may see with SORTBY

  • #NAME?: The file is open in Google Sheets or Excel 2019 or older.
  • #SPILL!: Something blocks the sorted list.

Questions

SORT or SORTBY?

In Excel, SORT sorts by a column inside the range (by number); SORTBY sorts by any range. See UNIQUE and SORT.

How do I get a top three?

Wrap it in TAKE: TAKE(SORTBY(A2:A9, C2:C9, -1), 3) in Excel 2024 or Microsoft 365.

Does SORTBY change the original data?

No. It returns a sorted copy that spills into new cells; the source stays as it is.

Can SORTBY sort by a column that isn't in the result?

Yes, that's its main use: SORTBY(A2:A9, C2:C9, -1) returns only the names, sorted by a sales column it doesn't return.

Does SORTBY keep sorting when the data changes?

Yes. It's a formula, so the sorted copy updates whenever the source or the sort column changes.

Sources

Official documentation the facts on this page were checked against.

Practise SORTBY on a live sheet

Lesson 39, UNIQUE and SORT (Unit 11, Dynamic arrays), has 6 graded exercises, checked instantly, with steps and hints when you're stuck. Pro