Organizing unstructured text data by character count is a common Excel hurdle, as native sorting features default strictly to alphabetical order. Traditionally, users rely on creating tedious helper columns utilizing the LEN function to calculate lengths before sorting.
Fortunately, modern dynamic array formulas eliminate this visual clutter, instantly streamlining your data structure. Stipulation: This advanced technique requires Excel 365 or Excel 2021 to support dynamic array behavior. For example, sorting a mixed list of product SKUs like "A1" and "C333" by length is now seamless. Below, we will detail the exact SORTBY formula to automate this workflow.
Excel is an incredibly powerful tool for organizing and analyzing data, but its built-in sorting features are primarily designed for alphabetical or numerical order. If you have ever tried to sort a list of keywords, product codes, or customer names by their character length, you probably noticed that there is no single button in the ribbon to achieve this.
Whether you are cleaning up a database, optimizing search engine optimization (SEO) keywords, or organizing part numbers, sorting text by string length is a highly practical skill. Fortunately, with the evolution of Excel's formula engine, this task has become remarkably easy. In this comprehensive guide, we will walk through the modern, dynamic way to sort text by length, as well as the traditional "helper column" approach for older versions of Excel.
If you are using modern Excel (Microsoft 365 or Excel 2021 and newer), you have access to dynamic array formulas. The absolute best way to sort text by string length without altering your original data is by combining the SORTBY and LEN functions.
To sort a range of cells based on character length, use the following syntax:
=SORTBY(array, LEN(array), [sort_order])
Let's break down how this works:
array: This is the range of cells containing the text strings you want to sort (e.g., A2:A10).LEN(array): The LEN function calculates the number of characters in each cell. By nesting this inside SORTBY, Excel creates an invisible list of numbers representing the length of each string, which it then uses to sort your primary data.[sort_order]: This optional parameter determines the direction of your sort. Use 1 for ascending order (shortest to longest) or -1 for descending order (longest to shortest). If omitted, Excel defaults to ascending order.Imagine you have a list of fruit names in cells A2:A8 that you want to sort from shortest to longest:
| Row | Column A (Original Data) |
|---|---|
| 2 | Strawberry |
| 3 | Fig |
| 4 | Banana |
| 5 | Watermelon |
| 6 | Kiwi |
| 7 | Apple |
| 8 | Pear |
To sort these fruits by length, select an empty cell (for example, C2) and type the following formula:
=SORTBY(A2:A8, LEN(A2:A8), 1)
Once you press Enter, Excel will automatically "spill" the sorted results into the cells below C2. Your output will look like this:
| Row | Column C (Sorted Output) | Character Count (for reference) |
|---|---|---|
| 2 | Fig | 3 |
| 3 | Kiwi | 4 |
| 4 | Pear | 4 |
| 5 | Apple | 5 |
| 6 | Banana | 6 |
| 7 | Strawberry | 10 |
| 8 | Watermelon | 10 |
Notice how strings of equal length (like "Kiwi" and "Pear") are grouped together. Excel preserves their original relative order when lengths are identical.
One common pitfall when sorting by string length is invisible white space. If "Kiwi " has an accidental trailing space, its length becomes 5 instead of 4, messing up your sort order. To prevent this, you can nest the TRIM function inside your formula to strip out extra leading, trailing, and double spaces:
=SORTBY(A2:A8, LEN(TRIM(A2:A8)), 1)
This ensures you are sorting by the actual length of the visible words, making your spreadsheet much more resilient to messy data entry.
If you are using Excel 2019, 2016, 2013, or earlier, you do not have access to the dynamic SORTBY function. If you try to use it, Excel will return a #NAME? error. Do not worry-you can easily achieve the exact same sorting outcome using a classic two-step process called a helper column.
Insert a new column next to your data. If your text is in Column A, you can use Column B as your helper column. In cell B2, write the standard length formula:
=LEN(A2)
Press Enter, then hover your mouse over the bottom-right corner of cell B2 until the cursor turns into a black cross. Double-click or drag it down to copy the formula to the bottom of your list. This column now clearly displays the numeric length of each text entry.
Once sorted, you can safely hide or delete the helper column if you no longer want to look at the numbers. (Note: If you delete the helper column entirely, make sure you convert the values to static values first, or copy and paste the sorted text to another location to avoid reference errors).
If your dataset contains empty cells, Excel will evaluate those blank cells as having a length of 0. In an ascending sort, these empty rows will float to the very top of your list, which can look messy and disrupt your reporting.
To bypass this in Excel 365, we can wrap our formula inside a FILTER function. This ensures that only non-blank cells are sorted:
=SORTBY(FILTER(A2:A10, A2:A10<>""), LEN(FILTER(A2:A10, A2:A10<>"")), 1)
By nesting FILTER(A2:A10, A2:A10<>""), we strip out any empty cells from the array before calculating the character length and sorting. The result is a clean list starting with your shortest actual word.
SORTBY + LEN combo if you are on Microsoft 365. It is dynamic, non-destructive, and automatically updates if you change the original text.TRIM() to avoid letting stray spaces throw off your rankings.Sorting by text length doesn't have to be a manual headache. By mastering these formulas, you can automate your data formatting, clean up messy databases, and unlock deeper insights into your textual data within seconds.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.