Sorting Text as Numbers

Written by Allen Wyatt (last updated December 31, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

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, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Horizontal Alignment Errors for Graphics

Discussion of alignment differences for graphics in Word 97 and Word 2000.

Discover More

Creating a Spelling Exclusion List

Don't like it when Word always says a word is spelled right and you know that it isn't? Here's how you can fine-tune the ...

Discover More

Moving Comment Background Pictures to Cells

When formatting comments, you can use a graphic as a background for the comment box. If you later want to move this ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Sorting while Ignoring Leading Characters

Want to ignore some characters at the beginning of each cell when sorting? The easiest way is to simply create other ...

Discover More

Sorting with Graphics

If you use graphics in a worksheet that are associated with certain cells (perhaps images of parts or icons for worksheet ...

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 ...

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 five more than 0?

2023-01-03 01:12:03

Tomek

Please note the space in the custom format i suggested:
# ??/??\"

Also, you can use "-" as the delimiter between inches and fractions displayed, but only in the format; when entering the data you have to use a ***single*** space like in 1 3/16.


2023-01-03 01:02:20

Tomek

If you use the last solution with custom format, you can even modify it to sort properly when the length is displayed in inches with fractions.
The custom format for this is
# ??/??\"
The nice thing about this format is that the spaces between whole inches and fractions will be aligned, as will be the / in fractions. and it will sort properly as the underlying numbers are stored as decimal numbers.
Once a column is formatted this way you can even enter your lengths explicitly as inches and fractions, which makes it easy to stick to 1/2 1/4 1/8ths...
For example you can enter 120 13/64.
If you try to enter decimals you may end up with fractions like 7/10 or 8/13 depending on nearest rounding.
If you need smaller fractions like 128ths just use more ? on both sides of the division mark.
(see Figure 1 below) for an example

Figure 1. 


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.