Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

Excel Tutorial

Excel HOME Excel Introduction Excel Get Started Excel Overview Excel Syntax Excel Ranges Excel Fill Excel Move Cells Excel Add Cells Excel Delete Cells Excel Undo Redo Excel Formulas Excel Relative Reference Excel Absolute Reference Excel Arithmetic Operators Excel Parentheses Excel Functions

Excel Formatting

Excel Formatting Excel Format Painter Excel Format Colors Excel Format Fonts Excel Format Borders Excel Format Numbers Excel Format Grids Excel Format Settings

Excel Data Analysis

Excel Sort Excel Filter Excel Tables Excel Conditional Format Excel Highlight Cell Rules Excel Top Bottom Rules Excel Data Bars Excel Color Scales Excel Icon Sets Excel Manage Rules (CF) Excel Charts

Excel PivotTables

Excel PivotTable Intro Excel Create PivotTable Excel PivotTable Values Excel PivotTable Filter Excel PivotTable Group Excel Calculated Field Excel PivotChart

Excel Case

Case: Poke Mart Case: Poke Mart, Styling

Excel Functions

AND AVERAGE AVERAGEIF AVERAGEIFS CONCAT COUNT COUNTA COUNTBLANK COUNTIF COUNTIFS DATEDIF FILTER HLOOKUP IF IFERROR IFS INDEX INDEX MATCH LEFT LEN LOWER MATCH MAX MEDIAN MID MIN MODE NETWORKDAYS NPV OR PROPER RAND RIGHT ROUND SORT STDEV.P STDEV.S SUBSTITUTE SUM SUMIF SUMIFS SUMPRODUCT TEXTJOIN TODAY TRIM UNIQUE UPPER VLOOKUP XLOOKUP XOR

Excel How To

Convert Time to Seconds Difference Between Times NPV (Net Present Value) Remove Duplicates

Excel Cert

Excel Certificate

Excel Examples

Excel Exercises Excel Syllabus Excel Study Plan Excel Training

Excel References

Excel Keyboard Shortcuts


Excel SORT Function


Share

SORT Function

The SORT function is a premade function in Excel, which returns a sorted copy of a range.

The sorted copy is placed where you type the formula. The original data is not changed.

This page is about the SORT function, which you type in a cell. To sort the data itself with the Sort commands in the ribbon, see the Excel Sort page.

It is typed =SORT and has the following parts:

=SORT(array, [sort_index], [sort_order], [by_col])

Note: SORT is available in Excel for Microsoft 365, Excel 2021, Excel 2024 and Excel for the web. It is not available in Excel 2019 or earlier. If you type it in an older version, you get a #NAME? error.

Note: The different parts of the function are separated by a symbol, like comma , or semicolon ;

The symbol depends on your Language Settings.

Array: The range to sort. Leave out the header row.

Sort_index: Optional. The number of the column to sort by, counted from the left of the array. The default is 1, the first column.

Sort_order: Optional. The order to sort in:

Sort_orderDescription
1Ascending, from smallest to largest (A to Z). This is the default.
-1Descending, from largest to smallest (Z to A).

By_col: Optional. FALSE sorts rows, which is the default. TRUE sorts columns, for data that goes across instead of down. You will rarely need it.

How to use the SORT function:

  1. Select a cell (G2)
  2. Type =SORT
  3. Double click the SORT command
  4. Mark the range to sort (A2:E21)
  5. Type (,)
  6. Type the number of the column to sort by (5)
  7. Type (,)
  8. Type the sort order (-1)
  9. Hit enter

Let's have a look at an example!

This is the same Pokemon table as on the XLOOKUP page. Copy it and paste it into cell A1 to follow along:

Example data

ABCDE
1ID#NameType 1Type 2Total
21BulbasaurGrassPoison318
32IvysaurGrassPoison405
43VenusaurGrassPoison525
54CharmanderFire309
65CharmeleonFire405
76CharizardFireFlying534
87SquirtleWater314
98WartortleWater405
109BlastoiseWater530
1110CaterpieBug195
1211MetapodBug205
1312ButterfreeBugFlying395
1413WeedleBugPoison195
1514KakunaBugPoison205
1615BeedrillBugPoison395
1716PidgeyNormalFlying251
1817PidgeottoNormalFlying349
1918PidgeotNormalFlying479
2019RattataNormal253
2120RaticateNormal413

SORT does not return the header row. Copy the headers in A1:E1 and paste them into G1.

Use the SORT function to sort the Pokemon by Total, from largest to smallest. Type the formula in G2:

ABCDEFGHIJK
1ID#NameType 1Type 2TotalID#NameType 1Type 2Total
21BulbasaurGrassPoison3186CharizardFireFlying534
32IvysaurGrassPoison4059BlastoiseWater0530
43VenusaurGrassPoison5253VenusaurGrassPoison525
54CharmanderFire30918PidgeotNormalFlying479
65CharmeleonFire40520RaticateNormal0413
76CharizardFireFlying5342IvysaurGrassPoison405
87SquirtleWater3145CharmeleonFire0405
98WartortleWater4058WartortleWater0405
109BlastoiseWater53012ButterfreeBugFlying395
1110CaterpieBug19515BeedrillBugPoison395
1211MetapodBug20517PidgeottoNormalFlying349
1312ButterfreeBugFlying3951BulbasaurGrassPoison318
1413WeedleBugPoison1957SquirtleWater0314
1514KakunaBugPoison2054CharmanderFire0309
1615BeedrillBugPoison39519RattataNormal0253
1716PidgeyNormalFlying25116PidgeyNormalFlying251
1817PidgeottoNormalFlying34911MetapodBug0205
1918PidgeotNormalFlying47914KakunaBugPoison205
2019RattataNormal25310CaterpieBug0195
2120RaticateNormal41313WeedleBugPoison195
=SORT(A2:E21, 5, -1)

Total is the 5th column of A2:E21, so sort_index is 5. Sort_order -1 sorts from largest to smallest.

Charizard has the highest Total (534). Caterpie and Weedle have the lowest (195).

The formula is typed only in G2. The result spills into G2:K21. Each row is kept together, so every Pokemon keeps its own Name, Types and Total.

Note: Empty cells in the array are returned as 0, not as empty cells. That is why Type 2 shows 0 for Pokemon like Charmander (row 15).

Note: The cells that the result spills into must be empty. If they are not, SORT returns a #SPILL! error.



Sort From A to Z

Leave out sort_index and sort_order to sort by the first column, from smallest to largest. For text, that is A to Z.

Sort the Pokemon names alphabetically. Type the label Name in M1, and this formula in M2:

M
1Name
2Beedrill
3Blastoise
4Bulbasaur
5Butterfree
6Caterpie
7Charizard
8Charmander
9Charmeleon
10Ivysaur
11Kakuna
12Metapod
13Pidgeot
14Pidgeotto
15Pidgey
16Raticate
17Rattata
18Squirtle
19Venusaur
20Wartortle
21Weedle
=SORT(B2:B21)

The array is only one column, so SORT sorts by that column. Beedrill comes first and Weedle last.


Sort a FILTER Result

You can put the FILTER function inside SORT. FILTER picks the rows, and SORT puts them in order.

List the Normal Pokemon, from the highest Total to the lowest. Replace the formula in G2 with this one:

GHIJK
1ID#NameType 1Type 2Total
218PidgeotNormalFlying479
320RaticateNormal0413
417PidgeottoNormalFlying349
519RattataNormal0253
616PidgeyNormalFlying251
=SORT(FILTER(A2:E21, C2:C21="Normal"), 5, -1)

FILTER returns the five Normal Pokemon in the same order as the table. SORT then sorts those rows by their 5th column, Total. Pidgeot (479) comes first and Pidgey (251) comes last.

Excel works from the inside out: first FILTER, then SORT.


Sort Command vs SORT Function

Sort commandSORT function
How you use itClick a Sort command in the ribbonType a formula in a cell
The resultThe data itself is put in a new orderA sorted copy, where you type the formula
When the data changesYou must sort againThe copy updates by itself
Excel versionsAll versionsMicrosoft 365, Excel 2021, Excel 2024 and Excel for the web

Excel also has a SORTBY function, which sorts a range by the values in another range, so the column you sort by does not have to be part of the result.

See also: FILTER returns the rows that match a condition, and UNIQUE lists the different values in a range.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.