Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Fixing Odd Sorting Behavior.

Fixing Odd Sorting Behavior

by Allen Wyatt
(last updated June 20, 2015)

1

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:

  • Numbers in increasing value (1, 2, 3, etc.)
  • Text in alphabetic order (a, b, c, etc.). If the text begins with a number (as in 38 Special), then the 3, as text, appears before the ABCs.
  • Logical values (False, True)
  • Error values (#DIV/0!, #N/A, etc.)
  • Blanks

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 (10767) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Fixing Odd Sorting Behavior.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Deleting Unwanted Styles

Custom styles can be a great help in formatting a worksheet. You may, at some point, want to get rid of all the custom styles ...

Discover More

Controlling Sorting Order

When you sort information in a worksheet, you have control over the order in which that information is sorted. Here's a quick ...

Discover More

Automatic Text in an E-mail

When creating an e-mail address hyperlink using the Insert Hyperlink dialog box, Excel allows you to enter a subject for the ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Sorting Letters and Numbers

Sorting information in a worksheet can be confusing when Excel applies sorting rules of which you are unaware. This is ...

Discover More

Controlling Sorting Order

When you sort information in a worksheet, you have control over the order in which that information is sorted. Here's a quick ...

Discover More

Moving Cell Borders when Sorting

Sort your data and you may be surprised at what Excel does to your formatting. (Some formatting may be moved in the sort and ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 4?

2017-01-17 14:38:45

Cathrine

I'm trying to do a multi level sort in excel.
I have three columns, month, year, and ID.
I want it to sort first by year, then by month, and then by ID#.

Under custom sort, I tell it to sort by year, lowest to highest.
then by month, and have it under a custom list that puts the months in order (january, february, march, etc), then ID number, A-Z (ID numbers start with letters, then have numbers at the end).

When I go to sort, everything looks great, except it sorted my months weird. For 2015, the months are "January, February, March, April, May, June, July, August, October, December, November, September"
For 2016, the order is "February, April, May, June, July, August, October, December, January, March, November, September"

it does it this way whether or not I add the third sort option.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.