ShareYourGroupShareYourGroupExcel Formulas

Dynamic arrays functions

SORT and SORTBY functions in Excel

SORT returns a range sorted by one of its columns; SORTBY sorts a range by the values of another range.

The Sort command rearranges your data where it sits, once. The SORT function leaves the data alone and shows a sorted copy somewhere else, which re-sorts itself whenever the data changes. It is available in Excel 2021 and Microsoft 365.

Syntax

=SORT(array, [sort_index], [sort_order], [by_col])
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
ArgumentWhat it is
arrayThe range to sort.
sort_indexWhich column of the range to sort by. 1 if left out.
sort_order1 for ascending (the default), -1 for descending.
by_colTRUE to sort columns from left to right. Leave it out to sort rows.
by_array1For SORTBY: the range whose values decide the order. It need not be part of what is returned.

Examples

Each result below is what Excel itself showed for the formula on the sample sheet.

Sample sheet: A small price list with item codes. The table is sorted by code.
ABCDE
1CodeItemCategoryPriceStock
2A101Basmati Rice 5 kgGrocery65040
3A102Toor Dal 1 kgGrocery16075
4B201NotebookStationery60300
5B202Ball PenStationery100
6C301LED BulbElectronics110200
7C302Power BankElectronics129918
8D401Steel BottleHome45055

How SORT works

Give SORT a range. It returns the same rows in order of the first column, smallest to largest or A to Z. The result spills from the formula's cell into the cells below and to the right.

The second argument chooses the column to sort by, counted from the left of the range you selected. The third sets the direction: 1 for ascending, -1 for descending.

Several levels

To sort by one column and break ties with another, give lists in curly brackets: =SORT(B2:D8,{2,3},{1,-1}) sorts by column 2 ascending, then by column 3 descending. The third example does this.

SORTBY

SORTBY separates two things that SORT ties together: what to show and what to sort by.

Whenever the sort key is outside the displayed columns, or the formula needs to survive changes to the layout, SORTBY is the better choice.

A custom order

Alphabetical order is not always wanted. To sort by size as Small, Medium, Large, give SORTBY the position of each value in your own list: =SORTBY(A2:C20, MATCH(B2:B20, {"Small","Medium","Large"}, 0)).

Sorting in random order

=SORTBY(A2:A20, RANDARRAY(19)) shuffles a list of nineteen names, which is useful for drawing lots or mixing quiz questions. It reshuffles on every recalculation, so copy the result and paste it as values when you want to keep one order.

Combined with other functions

Things to know

SORT or the Sort command?

Use the command on the Data tab to put the actual data in order. Use the function to keep the original entry order and show an ordered view for a report, a chart or a drop-down list.

In Google Sheets

Google Sheets has SORT with a slightly different shape: =SORT(range, sort_column, is_ascending, ...), where the direction is TRUE or FALSE and further pairs can follow. Sheets has no SORTBY, because its SORT can already sort by a range outside the data.

Related functions

Every result on this page was calculated in Microsoft Excel (version 16.0, build 20326) on 12 November 2026, not typed by hand.

This is an independent help site. It is not connected with or approved by Microsoft. Excel and Microsoft 365 are trademarks of Microsoft Corporation, named here only to say which program a formula is for. The people and figures in the sample sheets are made up.