Excel Sort by File Extension: How-To Guide
In this article
Sorting files in Excel by their extension (like .xlsx, .pdf, or .jpg) isn't a built-in one-click command, but you can do it with a helper column and a simple formula. This guide shows you the exact steps, the formula to use, and how to handle common problems like mixed file names and case sensitivity.
Why Excel Doesn't Have a Built-In "Sort by Extension" Button
Excel's sort feature works on the data inside your cells—numbers, text, dates—not on the file system properties of the files themselves. When you paste a list of file names into a spreadsheet, each name is just a text string. To sort by extension, you first need to extract the extension into its own column, then sort by that column. This is a common task for people who manage download folders, media libraries, or project files, and it's easy to do once you know the right formula.
Step-by-Step: Extract the Extension with a Formula
Assuming your file names are in column A (starting at A2), here's how to get the extension into column B:
- In cell B2, enter this formula:
=IFERROR(RIGHT(A2,LEN(A2)-FIND(".",A2)),"") - Press Enter. You'll see the extension (like "pdf") appear in B2.
- Drag the formula down to cover all rows with file names.
- If any cell shows
#VALUE!, it means the file name has no dot (e.g., a folder name). The IFERROR handles that by showing a blank. - Now select both columns A and B, go to the Data tab, and click Sort.
- In the Sort dialog, choose "Column B" as the Sort by column, and set Order to "A to Z". Click OK.
Your files are now grouped by extension. If you want to keep the original order but just see the extensions, you can skip the sort and just use the formula to view them.
What If File Names Have Multiple Dots?
The formula above works for most cases, but it fails when a file name contains a dot before the extension, like report.final.xlsx. The FIND function locates the first dot, so you'd get "final.xlsx" instead of "xlsx". To fix this, you need to find the last dot. Use this improved formula instead:
=IFERROR(RIGHT(A2,LEN(A2)-FIND("~",SUBSTITUTE(A2,".","~",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))),"")
This works by replacing the last dot with a tilde (~), then finding that position. It's a bit complex, but it handles any number of dots. For most users, the simple version is enough, but if you deal with versioned files or names with periods, use the robust one.
Sorting Without a Formula: Using Text to Columns
If you prefer not to use formulas, you can use Excel's Text to Columns feature to split the file name at the dot. Here's how:
- Select the column of file names.
- Go to the Data tab and click Text to Columns.
- Choose "Delimited" and click Next.
- Check "Other" and enter a period (.) in the box. Click Next.
- Choose "Text" as the column data format, then click Finish.
This splits the name into multiple columns: the first part before the dot, the second part after, and so on. The last column will contain the extension. However, this only works if your file names have exactly one dot—if there are multiple dots, you'll get extra columns. Also, this modifies your original data, so it's best to copy the list to a new column first.
Common Mistakes and How to Avoid Them
- Case sensitivity: Excel's sort is case-insensitive by default, so ".PDF" and ".pdf" sort together. If you need case-sensitive sorting, you'll need to use a formula like
=EXACT(B2,B2)—but that's rarely necessary. - Hidden extensions: Windows hides known extensions by default. If you don't see them, you need to enable "File name extensions" in File Explorer's View tab. Otherwise, your list might just be names without dots.
- Sorting the wrong column: Always ensure you select both columns before sorting, otherwise the file names and extensions will get mismatched.
- Using the simple formula on multi-dot names: As mentioned, it will extract the wrong part. Use the robust version if you see odd results.
Frequently Asked Questions
Can I sort by extension without a formula?
Yes, you can use Text to Columns to split the name at the dot, but it only works for single-dot names. For a more reliable method, the formula approach is better because it doesn't alter your original data and handles multi-dot names correctly.
Why does my sort not work after I add the formula?
Make sure you're sorting by the helper column (Column B) and not by the original file name column. Also, ensure that you've selected both columns so the rows stay together. If the formula returns an error for some rows, those will sort to the bottom, so check for #VALUE! errors.
How do I sort by extension in Excel for Mac?
The steps are identical. The formula and the Sort dialog work the same way in Excel for Mac. Just use the same formulas and the Data tab.
Can I sort by file type (like "PDF" vs "JPEG") instead of extension?
No, Excel doesn't know the file type, only the text in the cell. You'd need to have a column with the file type, or you could create a mapping formula that converts extensions to types (e.g., =IF(B2="pdf","Document",IF(B2="jpg","Image","Other"))). But that's more complex and not necessary for most sorting tasks.
What if I have a list of full file paths like C:\Folder\file.pdf?
You'll need to extract the file name first. Use a formula like =TRIM(RIGHT(SUBSTITUTE(A2,"\",REPT(" ",100)),100)) to get the last part after the backslash, then apply the extension formula to that. Or, use the robust extension formula directly on the full path—it will still find the last dot, which is what you want.