0Pricing
Excel Formulas Academy · Lesson

Sorting by Another Column With SORTBY

Order one array using the values from a different array.

Sorting by Another Column With SORTBY is a free Excel Formulas Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Excel Formulas Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why SORTBY Exists

SORT orders data by a column that is inside the array. But what if you want to order one list by the values in a separate array that is not part of the output?

That is exactly what SORTBY does. It sorts one range using the order of completely different ranges, giving you flexible, multi-key sorting without including the sort columns in your results.

=SORTBY(A2:A10, B2:B10)

The SORTBY Syntax

SORTBY's pattern is:

  • array the data you want returned, in sorted order
  • by_array1 the values that decide the order
  • sort_order1 1 for ascending, -1 for descending (optional)

You can keep adding by_array / order pairs to sort by several keys. Each by_array must be the same height (or width) as the array being sorted.

=SORTBY(array, by_array1, [order1], [by_array2], [order2], ...)

Sorting Names by a Hidden Score

Say A2:A10 holds player names and B2:B10 holds their scores. You want a list of names ordered by score, highest first, but you only want the names to show.

SORTBY returns just the names, ordered by the score array. The scores themselves never appear in the output.

=SORTBY(A2:A10, B2:B10, -1)

SORTBY vs SORT

The key difference: with SORT, the column you sort by is part of the result. With SORTBY, the sort key lives in a separate array and is not returned.

  • Use SORT when the order column should appear in the output
  • Use SORTBY when you want a clean output ordered by something behind the scenes
=SORTBY(A2:A10, C2:C10, 1)

Sorting a Whole Table by an Outside Column

The array you return can be multiple columns. Suppose A2:B10 holds Name and Email, and C2:C10 holds a priority number.

SORTBY returns the Name/Email table ordered by priority, even though priority is never shown in the result.

=SORTBY(A2:B10, C2:C10, 1)

Sorting by Multiple Keys

Add more by_array / order pairs to break ties. SORTBY applies them in order: the first key sorts first, the second key settles any ties, and so on.

Here the table sorts by Region ascending, then by Sales descending within each region.

=SORTBY(A2:C20, B2:B20, 1, C2:C20, -1)

Sorting by a Calculated Array

The by_array does not have to be a stored column. You can sort by a calculation done on the fly.

Imagine A2:A10 holds product names and B2:B10 price, C2:C10 cost. To order products by profit margin without storing it anywhere, compute it inside SORTBY.

=SORTBY(A2:A10, B2:B10-C2:C10, -1)

Keeping Arrays the Same Size

Every by_array must match the length of the array you are sorting. If the array has 9 rows, each by_array must also have 9 rows.

A mismatch produces a #VALUE! error. Double-check your ranges start and end on the same rows.

=SORTBY(A2:A10, B2:B10, -1)

SORTBY in Google Sheets

Google Sheets supports SORTBY with the same syntax, so your formulas are portable. The behavior, including multiple sort keys and calculated by_arrays, matches Excel.

Just like SORT, SORTBY spills its results, so make sure the cells below have room.

=SORTBY(A2:C20, B2:B20, 1, C2:C20, -1)

Combining SORTBY With FILTER

Like SORT, SORTBY plays well with FILTER. Filter first to keep only the rows you want, then SORTBY orders what remains.

Note: when you filter the array, you must filter the by_array the same way so they stay aligned. Here we sort the full table by a filtered helper only if both share the same rows, so often it is simpler to filter the result of SORTBY instead.

=FILTER(SORTBY(A2:C20, C2:C20, -1), SORTBY(B2:B20, C2:C20, -1)="East")

A Practical Leaderboard

Put it together: a live leaderboard that lists player names ordered by score, breaking ties by fewest attempts.

With names in A2:A20, scores in B2:B20, and attempts in C2:C20, SORTBY ranks by score (high first) then attempts (low first). Update any value and the order refreshes.

=SORTBY(A2:A20, B2:B20, -1, C2:C20, 1)

Quick Check

Names are in A2:A10 and their scores are in B2:B10. You want only the names returned, ordered from highest score to lowest. Which formula works?

Recap: Sorting With SORTBY

SORTBY orders one array using the values of separate arrays that are not shown in the result:

  • =SORTBY(array, by_array1, order1, ...)
  • Add more by_array / order pairs to sort by multiple keys
  • by_arrays can be stored columns or live calculations
  • Every by_array must match the length of the array

It is the go-to for clean outputs ordered by a behind-the-scenes value, like a leaderboard.

=SORTBY(A2:A20, B2:B20, -1, C2:C20, 1)

Frequently asked questions

Is the “Sorting by Another Column With SORTBY” lesson free?

Yes — the full text of “Sorting by Another Column With SORTBY” is free to read here on the web, and the Excel Formulas Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Excel Formulas Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sorting by Another Column With SORTBY”?

Order one array using the values from a different array. You practise Excel Formulas Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Excel Formulas Academy?

No prior experience is required. Excel Formulas Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sorting by Another Column With SORTBY” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Excel Formulas Academy lesson?

Yes. Every Excel Formulas Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Ordering Results With SORT
  2. Sorting by Another Column With SORTBY
  3. Generating Numbers With SEQUENCE
  4. Creating Random Data With RANDARRAY
← Back to Excel Formulas Academy