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: Sorting Decimal Values.

Sorting Decimal Values

Written by Allen Wyatt (last updated March 18, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Bob often needs to construct tables that are keyed to titles in government regulations. The numbering of the regulations is in decimal form and this creates problems when he tries to sort them in order. Examples are 820.20, 820.25, 820.200, 820.250. Bob enters these as text, but they still come out sorted in a manner that he does not want. In all cases, Excel drops off the trailing zeros and sees "820.20" and "820.200" as the same thing; Bob is wondering what he can do.

First of all, it should be pointed out that if Excel is dropping the trailing zeroes, then the cells are not formatted as text. You'll need to format the cells as text before you put anything in them, or else you'll need to precede the entry with an apostrophe. In either case, the trailing zeroes should remain in place.

Another way to force the entries to text is to modify them in some way. For instance, you could enter "Reg 820.200" instead of "820.200." Or you could replace the period after the 820 with a space or a dash. Any of these methods, and many more, would force the entry to be treated as text.

Even if you force the entry of information to text, that still won't solve the sorting problem, however. Sort a bunch of these cells, and they will still come out in an order you don't want:

820.190
820.2
820.20
820.200
820.201
820.25
820.27

The reason is because the sorting is done from left to right, and in this scheme ".20" will always come before ".200" which always comes before ".25." The only way around this is to modify the structure of the numbers so that (in this case) there are always three digits after the decimal point:

820.002
820.020
820.025
820.027
820.190
820.200
820.201

While this gives the proper sorting order, it does havoc to the original intent: to match the numbering used in the governmental numbering system. If you want to be true to that numbering scheme, the only solution is to use three columns for your numbering. The first column would be the government numbers, entered as text. The second column would be the part those numbers to the left of the decimal point, derived with a formula:

=LEFT(A1,FIND(".",A1)-1)

The third column would be the portion to the right of the decimal point, derived with this formula:

=RIGHT(A1,LEN(A1)-FIND(".",A1))

With the three columns in place, you can then do your sorting based on the contents of the second and third columns. After the numbers are sorted, you can hide the second and third columns, as desired.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3836) 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: Sorting Decimal Values.

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

Automatically Opening a Document at a Specific Zoom Setting

Do you prefer to have your documents open at a specific zoom magnification? You can get whatever magnification you desire ...

Discover More

Using a Macro to Change the Formatting of All Instances of a Word

If you have a word that you need to make sure is formatted the same way throughout your document, there are several ways ...

Discover More

Weighted Averages in a PivotTable

PivotTables are used to boil down huge data sets into something you can more easily understand. They are very good simple ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Sorting by Columns

When you think of sorting Excel data, it is likely that you think of sorting rows. Excel also allows you to sort by ...

Discover More

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

Sorting Dates by Month

Sorting by dates is easy, and you end up with a list that is in chronological order. However, things become a bit more ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 2 + 8?

2020-10-22 16:30:41

Julie

This did not work for me. I have regulation 5.3 and 5.30 and they both turned into "3" in the "right" formula. =RIGHT(A1,LEN(A1)-FIND(".",A1))


2017-12-11 11:28:35

Mo Williams

Hi Allen,

I'm trying to do something similar to the above but with a 5 level hierarchy, i.e.

1
1.1
1.10
1.11
1.12
1.13
1.14
1.15
1.16
1.17
1.18
1.19
1.2
1.20
1.21
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.1.1.1.1
1.1.2
1.1.3
1.2.1
1.2.2
1.2.2.1
1.2.2.2
1.2.2.1.1
1.2.2.1.2

but excel sorts it like this

1
1.1
1.1.1.1.1
1.1.2
1.1.3
1.10
1.11
1.12
1.13
1.14
1.15
1.16
1.17
1.18
1.19
1.2
1.2.1
1.2.2
1.2.2.1
1.2.2.2
1.2.2.2.1
1.2.2.2.2
1.2.2.2.3
1.20
1.21
1.3
1.4
1.5
1.6
1.7
1.8
1.9

can I expand your formulas above to apply to five levels?

Thanks in advance,

Mo


This Site

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.

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.