Sorting Text as Numbers

by Allen Wyatt
(last updated August 5, 2017)

6

Kyle has a worksheet that consists of three columns of data: Part Number, Quantity, and Length. Length is designated in inches, with a quotation mark to show it is in inches (such as 30", 54", or 100"). Kyle needs to sort the data from shortest to longest length, but Excel sorts the lengths as text, such that 100" comes before 30". He wonders if there is a way to get Excel to sort the textual information as if it were numbers, so 30" correctly comes before 100", without getting rid of the quotation marks.

The short answer is that you can't do it, at least not directly. When you include the quote mark in the cell, Excel treats the entire cell as text and sorts it as such. And, as Kyle noted, the text string 100" comes before 30" because 1 comes before 3 in a textual sort.

There are things you can do, however. For instance, if you use a formula to generate the values in your Length column, you could modify the formula so that it "front pads" the lengths with zeroes, as necessary. Using this approach you would not have lengths like 30", 54", or 100", but instead you would have 030", 054", and 100". As long as all the lengths used the same number of digits, the sorting will occur correctly.

You could also add a helper column to the right of the Length column and, in the helper column, place the numeric values of whatever is in the cell to the left. So, if your first data in in cell C2, in cell D2 (your helper column) you could enter the following:

=VALUE(SUBSTITUTE(C2,"""",""))

The four quote marks are necessary as the second parameter of the SUBSTITUTE function in order to get rid of quote marks in C2. The result is that D2 contains the numeric value of whatever was in cell C2. Copy the formula down as far as necessary, and then use column D to do the sorting. After sorting you can even hide column D, if desired, or make it as narrow as you want.

Another approach that can work well if you have a limited number of lengths is to create a custom list and then use that list to do the sorting. Here's how to set up the custom list for your lengths:

  1. Display the Excel Options dialog box. (In Excel 2007 click the Office button and then click Excel Options. In Excel 2010 or later versions, display the File tab of the ribbon and then click Options.)
  2. If you are using Excel 2007, make sure Popular is selected at the left of the dialog box. If you are using Excel 2010 or a later version, click Advanced and then scroll toward the end of the options until you see the General area.
  3. Click Edit Custom Lists. Excel displays the Custom Lists dialog box and hides the Excel Options dialog box. (See Figure 1.)
  4. Figure 1. The Custom Lists dialog box.

  5. Select NEW LIST in the Custom Lists list.
  6. In the List Entries portion of the dialog box, start typing the items in your list, in the order they should appear. For instance, if you have only 15 possible lengths, type all the lengths, in their proper order, makng sure to include the quote mark after each length. Press Enter at the end of each length you add.
  7. When you are done, click the Add button.
  8. Click OK to close the Custom Lists dialog box. The Excel Options dialog box reappears.
  9. Click OK to close the Excel Options dialog box.

Once the custom list is defined, you can use the Sort dialog box and specify that you want to do a custom sort. Pick your new list and that is what Excel will use when arranging the rows in the worksheet. Again, this approach only really works if you have a limited number of lengths and you know ahead of time what those lengths will be.

Perhaps the best solution, though, is to remove the quotation marks. (Yes, I know... Kyle said he didn't want to get rid of them, but bear with me for a moment.) If everything in the Length column is shown in inches, then you can get rid of the explicit quote marks and create a custom format that will display them. Getting rid of the quote marks is easy—just use Find and Replace to remove them. (Search for a quote mark and replace it with nothing.) Then, create the custom format in this manner:

  1. Select all the cells that contain lengths. (You can select the entire column, if desired.)
  2. Display the Home tab of the ribbon.
  3. Click the small icon at the lower-right corner of the Number group. Excel displays the Format Cells dialog box, with the Number tab selected.
  4. In the Category list, choose Custom. The dialog box changes so you can enter a custom format. (See Figure 2.)
  5. Figure 2. The Number tab of the Format Cells dialog box.

  6. In the Type box, enter the following: 0.0\"
  7. Click OK.

You should now see all the cells (selected in step 1) displayed with the trailing quote mark. It is the format specified in step 5 that instructs Excel to include the quote mark after any number. It also instructs Excel to display one digit after the decimal point. (You can modify the format so it displays a different number of digits, if desired. If you just want the number to be generally visible, then use this custom format:

#\"

As with the custom format in step 5 of the steps just shown, the backslash is necessary so that Excel knows the quote mark is part of the custom format itself.

Again, the custom format route works great if everything in the Length column is shown in inches. If it isn't all in inches, then you could move the dimension indicator (quote mark, apostrophe, whatever) to column D and remove it from column C. Make column D just as narrow as necessary to display the dimension indicator, and then sort by column C.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3917) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

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

Accurate Font Sizes

Want to get your typeface exactly the right size? Here's how you can specify just the size you want Word to use.

Discover More

Counting All Graphics

Need to know how many graphics a document contains? Getting at the true number may take a little more work than it first ...

Discover More

What is a Desktop.ini File?

When browsing through your system, you might see several files named Desktop.ini. This tip explains what these files are and ...

Discover More

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!

More ExcelTips (ribbon)

Incomplete and Corrupt Sorting

Using the sorting tools, on the toolbar, may result in some unwanted results, such as jumbled data. If this happens to you, ...

Discover More

Sorting Dates and Times

One of the strong features of Excel is its ability to sort information in a worksheet. When it doesn't sort information as ...

Discover More

Sorting by the Last Digits in Variable Length Data

Excel is great at sorting information in a worksheet. Sometimes it can be a bit confusing as to how to set up the sort based ...

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 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 two more than 4?

2017-08-07 04:32:22

David Robinson

Guy,
To embellish Rod's explanation on the four double quotes, the meaning of each quote is:
first one = opens string
second one = an ESCAPE CHARACTER to tell Excel to treat the next character as it is, and not to treat it as closing the string
third one = the actual character to substitute
fourth one = closes string.

Hope this helps. If you're unclear on escape characters they're worth a quick read, especially as my preferred option, the format mask, uses the \ escape character.


2017-08-06 02:38:39

Aldo

Use a number format to display inches like . . .

# ##/##\";[Red]- # ##/##\";0\"

This will display whole numbers and fractions properly in inch format.


2017-08-05 09:06:07

Sheryl Lucas

If he changed the column label to "length (in inches)" or "length (in.)" it would eliminate the need for the quote mark altogether.


2017-08-05 08:54:19

Allen

Guy: I would, too, think it would make sense to use only three quote marks. (Or, better still, a quote followed by a backslash, and then two more quotes.) As Rod notes, though, it needs to be four quote marks.

-Allen


2017-08-05 06:34:39

Rod Grealish

The quote (") is being used for two purposes, as a string delimiter and as a literal character. In this case the literal quote is doubled ("") to show that it is not to be interpreted as closing the string. This is a common problem in many programming languages where a character has a special usage in the language and also needs to be used literally without its special usage. The problem arises in wildcard searches in Word where some characters have special usages such as [,{,(. If you want to match one of these characters in a search then you need to precede it by a \ - which also means that to match a \ you need to write it as \\.


2017-08-05 05:33:59

Guy

Hello Allen:

Thanks for all the great ideas you share. In this formula, =VALUE(SUBSTITUTE(C2,"""","")), shouldn't there just be 3 quotes in the instead of 4 since 30" only has 1 quotation mark?


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.