Sort data in a range or table
Sorting data is an integral part of data analysis. You might want to put a list of names in alphabetical order, compile a list of product inventory levels from highest to lowest, or order rows by colors or icons. Sorting data helps you quickly visualize and understand your data better, organize and find the data that you want, and ultimately make more effective decisions.
Note To find the top or bottom values in a range of cells or table, such as top 10 grades or bottom 5 sales amounts, use AutoFilter or conditional formatting. For more information, see Filter data in a range or table and Add, change, or clear conditional formats.What do you want to do?
Sort by cell color, font color, or icon
Sort by more than column or row
Sort one column in a range of cells without affecting the others
Learn more about general issues with sorting
Learn about sorting
You can sort data by text (A to Z or Z to A), numbers (smallest to largest or largest to smallest), and dates and times (oldest to newest and newest to oldest) in one or more columns. You can also sort by a custom list (such as Large, Medium, and Small) or by format, including cell color, font color, or icon set. Most sort operations are column sorts, but you can also sort by rows.
Sort criteria are saved with the workbook so that you can reapply the sort each time that you open the workbook for an Excel table, but not for a range of cells. If you want to save sort criteria so that you can periodically reapply a sort when you open a workbook, then it's a good idea to use a table. This is especially important for multicolumn sorts or for sorts that take a long time to create.
Sort text
- Select a column of alphanumeric data in a range of cells, or make sure that the active cell is in a table column containing alphanumeric data.
- On the
Hometab, in theEditinggroup, and then clickSort & Filter.
- Do one of the following:
- To sort in ascending alphanumeric order, click
Sort A to Z. - To sort in descending alphanumeric order, click
Sort Z to A.
- To sort in ascending alphanumeric order, click
- Optionally, you can do a case-sensitive sort.
How to sort by case-sensitivity
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then clickCustom Sort. - In the
Sortdialog box, clickOptions. - In the
Sort Optionsdialog box, selectCase sensitive. - Click
OKtwice.
- On the
Issue: Check that all data is stored as text If the column that you want to sort contains numbers stored as numbers and numbers stored as text, then you need to format them all as text. If you do not, the numbers stored as numbers are sorted before the numbers stored as text. To format all of the selected data as text, on the Home tab, in the Font group, click the Format Cell Font button, click the Number tab, and then under Category, click Text.
Issue: Remove any leading spaces In some cases, data imported from another application might have leading spaces inserted before data. Remove the leading spaces before sorting the data.
Sort numbers
- Select a column of numeric data in a range of cells, or make sure that the active cell is in a table column containing numeric data.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then do one of the following:- To sort from low numbers to high numbers, click
Sort Smallest to Largest. - To sort from high numbers to low numbers, click
Sort Largest to Smallest.
- To sort from low numbers to high numbers, click
Issue: Check that all numbers are stored as numbers If the results are not what you expected, the column might contain numbers stored as text and not as numbers. For example, negative numbers imported from some accounting systems or a number entered with a leading ' (apostrophe) are stored as text. For more information, see Convert numbers stored as text to numbers.
Sort dates or times
- Select a column of dates or times in a range of cells, or make sure that the active cell is in a table column containing dates or times.
- Select a column of dates or times in a range of cells or table.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then do one of the following:- To sort from an earlier to a later date or time, click
Sort Oldest to Newest. - To sort from a later to an earlier date or time, click
Sort Newest to Oldest.
- To sort from an earlier to a later date or time, click
Issue: Check that dates and times are stored as dates or times If the results are not what you expected, the column might contain dates or times stored as text and not as dates or times. For Excel to sort dates and times correctly, all dates and times in a column must be stored as a date or time serial number. If Excel cannot recognize a value as a date or time, the date or time is stored as text. For more information, see Convert dates stored as text to dates.
Note If you want to sort by days of the week, format the cells to show the day of the week. If you want to sort by the day of the week regardless of the date, convert them to text by using the TEXT function. However, the TEXT function returns a text value, and so the sort operation would be based on alphanumeric data. For more information, see Show dates as days of the week.
Sort by cell color, font color, or icon
If you have manually or conditionally formatted a range of cells or table column, by cell color or font color, you can also sort by these colors. You can also sort by an icon set created through a conditional format.
- Select a column of data in a range of cells, or make sure that the active cell is in a table column.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then clickCustom Sort.
The
Sortdialog box is displayed. - Under
Column, in theSort bybox, select the column that you want to sort. - Under
Sort On, select the type of sort. Do one of the following:- To sort by cell color, select
Cell Color. - To sort by font color, select
Font Color. - To sort by an icon set, select
Cell Icon.
- To sort by cell color, select
- Under
Order, click the arrow next to the button, and then, depending on the type of format, select a cell color, font color, or cell icon. - Under
Order, select how you want to sort. Do one of the following:- To move the cell color, font color, or icon to the top or left, select
On Topfor a column sort, andOn Leftfor a row sort. - To move the cell color, font color, or icon to the bottom or right, select
On Bottomfor a column sort, andOn Rightfor a row sort.
Note There is no default cell color, font color, or icon sort order. You must define the order that you want for each sort operation.
- To move the cell color, font color, or icon to the top or left, select
- To specify the next cell color, font color, or icon to sort by, click
Add Level, and then repeat steps three through five.Make sure that you select the same column in the
Then bybox and that you make the same selection underOrder.Keep repeating for each additional cell color, font color, or icon that you want included in the sort.
Sort by a custom list
You can use a custom list to sort in a user-defined order. Excel provides built-in, day-of-the-week and month-of-the year custom lists, and you can also create your own custom list.
- Optionally, create the custom list.
How to create a custom list
- In a range of cells, enter the values that you want to sort by, in the order that you want them, from top to bottom. For example:
A 1 High 2 Medium 3 Low - Select the range that you just typed. In the example above, you would select cells A1:A3.
- Click the
Microsoft Office Button
, click Excel Options, click thePopularcategory, and then underTop options for working with Excel, clickEdit Custom Lists. - In the
Custom Listsdialog box, clickImport, and then clickOKtwice.
Notes:
- You can only create a custom list based on a value (text, number, and date or time). You cannot create a custom list based on a format (cell color, font color, and icon).
- The maximum length for a custom list is 255 characters, and the first character must not begin with a number.
- In a range of cells, enter the values that you want to sort by, in the order that you want them, from top to bottom. For example:
- Select a column of data in a range of cells, or make sure that the active cell is in a table column.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then clickCustom Sort.
The
Sortdialog box is displayed. - Under
Column, in theSort byorThen bybox, select the column that you want to sort by a custom list. - Under
Order, selectCustom List. - In the
Custom Listsdialog box, select the list that you want. In the preceding example, you would clickHigh, Medium, Low. - Click
OK.
Sort rows
- Select a row of data in a range of cells, or make sure that the active cell is in a table column.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then clickCustom Sort.
The
Sortdialog box is displayed. - Click
Options. - In the
Sort Optionsdialog box, underOrientation, clickSort left to right, and then clickOK. - Under
Column, in theSort bybox, select the row that you want to sort. - Do one of the following:
By value
- Under
Sort On, selectValues. - Under
Order, do one of the following:- For text values, select
A to ZorZ to A. - For number values, select
Smallest to LargestorLargest to Smallest. - For date or time values, select
Oldest to NewestorNewest to Oldest.
- For text values, select
By cell color, font color, or cell icon
- Under
Sort On, selectCell Color,Font Color, orCell Icon. - Click the arrow next to the button, and then select a cell color, font color, or cell icon.
- Under
Order, selectOn LeftorOn Right.
- Under
Note When you sort rows that are part of a worksheet outline, Excel sorts the highest-level groups (level 1) so that the detail rows or columns stay together, even if the detail rows or columns are hidden.
Sort by more than column or row
You might sort by more than one column or row when you have data that you want to group by the same value in one column or row, and then sort another column or row within that group of equal values. For example, if you have a Department and Employee column, you can first sort by Department (to group all the employees in the same department together), and then sort by name (to put the names in alphabetical order within each department). You can sort by up to 64 columns.
Note For best results, the range of cells that you sort should have column headings.
- Select a range of cells with two or more columns of data, or make sure that the active cell is in a table with two or more columns.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then clickCustom Sort.
The
Sortdialog box is displayed. - Under
Column, in theSort bybox, select the first column that you want to sort. - Under
Sort On, select the type of sort. Do one of the following:- To sort by text, number, or date and time, select
Values. - To sort by format, select
Cell Color,Font Color, orCell Icon.
- To sort by text, number, or date and time, select
- Under
Order, select how you want to sort. Do one of the following:- For text values, select
A to ZorZ to A. - For number values, select
Smallest to LargestorLargest to Smallest. - For date or time values, select
Oldest to NewestorNewest to Oldest. - To sort based on a custom list, select
Custom List.
- For text values, select
- To add another column to sort by, click
Add Level, and then repeat steps three through five. - To copy a column to sort by, select the entry, and then click
Copy Level. - To delete a column to sort by, select the entry, and then click
Delete Level.Note You must keep at least one entry in the list.
- To change the order in which the columns are sorted, select an entry, and then click the
UporDownarrow to change the order.Entries higher in the list are sorted before entries lower in the list.
Sort one column in a range of cells without affecting the others
Warning Be careful when using this feature. Sorting by one column in a range may produce results that you don't want, such as moving cells in that column away from other cells in the same row.
Note You cannot do the following procedure in a table.
- Select a column in a range of cells containing two or more columns.
- To select the column that you want to sort, click the column heading.
- On the
Hometab, in theEditinggroup, clickSort & Filter, and then click one of the available sort commands. -
The Sort Warning dialog box is displayed.
- Select Continue with the current selection.
- Click Sort.
- Select any other sort options that you want in the
Sortdialog box, and then clickOK.
If the results are not what you want, click Undo
.
Learn more about general issues with sorting
If you get unexpected results when sorting your data, do the following:
Check to see if the values returned by a formula have changed If the data that you have sorted contains one or more formulas, the return values of those formulas can change when the worksheet is recalculated. In this case, make sure that you reapply the sort or do the sort again to get up-to-date results.
Unhide rows and columns before you sort Hidden columns are not moved when you sort columns, and hidden rows are not moved when you sort rows. Before you sort data, it's a good idea to unhide the hidden columns and rows.
Check the locale setting Sort orders vary by locale setting. Make sure that you have the proper locale setting in Regional Settings or Regional and Language Options in Control Panel on your computer. For information about changing the locale setting, see the Windows help system.
Enter column headings in only one row If you need multiple line labels, wrap the text within the cell.
Turn on or off the heading row It's usually best to have a heading row when you sort a column to make it easier to understand the meaning of the data. By default, the value in the heading is not included in the sort operation. Occasionally, you may need to turn on or off the heading so that the value in the heading is or is not included in the sort operation. Do one of the following:
- To exclude the first row of data from the sort because it is a column heading, on the
Hometab, in theEditinggroup, clickSort & Filter, clickCustom Sort, and then select My data has headers. - To include the first row of data in the sort because it is not a column heading, on the
Hometab, in theEditinggroup, clickSort & Filter, clickCustom Sort, and then clear My data has headers.