
Tips.Net > ExcelTips Home > Sorting > Fixing Odd Sorting Behavior
Summary: When you sort data that contains both numbers and text, you may not get exactly the result that you expected. To know what is going on, you have to know how Excel views your data. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
Michael runs a karaoke company and uses Excel to create his song books. The worksheet contains three columns for song number, song title, and artist. Michael runs into odd behavior when sorting the song book by either artist or title.
For instance, when he sorts by artist the group 311 will come up in two different spots—four of their songs are placed right after the band 112 and before 702, and then it sorts the rest right after 3 of Hearts and before 38 Special. Then, when sorting by song, George Strait's song "True" always ends up as the last song in the list.
This obviously isn't want Michael wants to see happen when sorting. The reason it is happening, however, is due to the way that Excel interprets the information in each cell. When you enter information in a cell, Excel tries to parse that information and determine if it is a number, a date, or text. It just so happens that Excel is "guessing wrong" when it comes to some group and song titles.
When you enter the group 311, Excel considers that a number, so it treats it as a number. Similarly, when you enter the song title "True," Excel considers that a Boolean value—a number. (It would do the same thing if you had a song named "False.")
When performing a sorting, Excel first sorts by the data type and then within the data type. 112 and 702 are numbers. 3 of Hearts and 38 Special are text because they don't consist of only digits. When sorting by artist, the group 311 shows up in two different places because the group name was parsed by Excel in some instances as a number and in other instances as text.
To understand how to correct the odd behavior, it is important to understand that the behavior isn't really odd; it is the logic Excel uses. If you want different results, you have to work with your data to make sure it is not parsed incorrectly by Excel.
First, if you sort in ascending order, the values in your cells will be sorted in these data types:
If you sort in descending order, then the order is the reverse of what is shown here, except that blanks still appear as the last data type sorted.
You can better see the data types that Excel assigns to various cells by removing any explicit alignment in the cells. By default the text values will be left-justified, numbers right-justified, and Boolean and error values centered.
To get things to sort the way you want, you just need to make sure that all the cells in a column contain the same type of data. In the case of both artist and song title, this would be text. In the cells being sorted as numbers (like 311), edit the cell to place an apostrophe before the first digit in the number. This tells Excel you want the cell's value treated as text. You can also do the same thing with "True."
Tip #3362 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Got the Time? If you work with either times or dates in Excel, you really need ExcelTips: Times and Dates. Everything you need to know about slicing, dicing, and generally working with times and dates.
If you have tons of data to analyze, one of the best tools in Excel's arsenal is the PivotTable. Learn how to use this tool to analyze your data. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site