Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Fixing Odd Sorting Behavior.
Written by Allen Wyatt (last updated July 29, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
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 sort, 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 are 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."
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3362) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Fixing Odd Sorting Behavior.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
As yo get more and more worksheets into a workbook, you'll find yourself moving them around into different sequences. You ...
Discover MoreUsing the sorting tools, on the toolbar, may result in some unwanted results, such as jumbled data. If this happens to ...
Discover MoreNeed to sort your data based on the color of the cell? Excel doesn't include this capability, but you can still do the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-09-07 07:05:03
Linda
Dear Allen,
I have regularly used your help, but this time I've fixed my sorting issue in Excel myself after consulting many resources in vein. i'd like to share it with you, as it may help others:
My worksheet contained some invisible subtotals information, and after removing all subtotals I was able to sort dates, alphabetical, numbers etc. as usual without problems!
2017-04-05 18:17:25
joe
although I created a custom sort excel ignores it and sorts by alphabet? i am using excel 2013
2017-02-01 05:55:13
Christopher Davis
Thanks for taking the time and trouble.
We have Excel 2003 which is menus not ribbons.
I always align using the icons on the toolbar and, to be perfectly honest, it never occured to me to click any of these icons twice! Today I did and, surprise, surprise, the alignment vanished! (Or basically defaulted to the degault!)
I appreciate that the answer is in the last paragraph but my problem is that I want the numbers to be numbers and I'd like to be able to format the whole column so that this happens, and I can't work out how to do this. However, I have just added a new column and filled it with "=istext()" so I can see the guilty cells, I now plan to sort on the Trues and Falses and will manually type over the text numbers to make them proper numbers.
It's a bit of a long-winded work around but sometimes we have to go that way.
Thanks for the clues and tips.
Chris
2017-01-31 12:09:20
Willy Vanhaelen
@Chris Davis
In the Home tab, Alignment section, second row left, there are 3 buttons:
- left align - center align - right align
When none of these buttons are highlighted Excel will apply the default alignment for that cell: left for text and right for numbers.
You can override this default behaviour for that cell (or selection range) by clicking on one of the 3 buttons. That button becomes highlighted and your entry will always be aligned that way whether it is text or a number.
If you click the same (highlighted) alignment button again, the highlight disappears and the default alignment is restored (left for text, right for a number). That is removing the 'explicit alignment'.
As for your second question, read carefully the last paragraph, the answer is there.
2017-01-30 06:58:45
Chris Davis
We have a spreadsheet with a similar problem, so can you tell me how to remove any 'explicit alignment' in the cells, please.
I have to add that I have already worked out which are the text numbers and which are the number numbers but my problem is trying to make them all the same! I think I have tried everything, including format painter.
Any suggestions will be gratefully received. Thanks in anticipation.
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments