Author: Oscar Cronquist Article last updated on May 31, 2021

Sort values by corresponding text arranged in a column

This article describes a formula that sorts values arranged in a column from A to z by every other value. The first value in column B, see image above, is a text value. The next value is a number, the formula in column D sorts the values in column B by the text values. The number below each text value is also sorted based on the corresponding text value.

1. Sort items by adjacent number in every other value - Array formula

I showed you in an earlier post how to sort text by number using a formula, it was a question from Denisa. The first thing that comes to mind would be to rearrange the values and then apply a filter or an excel defined table to be able to sort the names by value.

In other words, names in one column and numbers in another column. But I didn't, I built a formula, shown in row 3 (A3:F3), it was an interesting challenge that I could not resist.

15 is the largest number and Zack is the corresponding name. 13 is the second largest number and John is the name next to it. 10 is the smallest number and the adjacent name is Nick. Check out the post if you want to know more.

In this post, I will show you a formula that sorts numbers by text, a little bit different than the previous post mentioned above. The values are in column A and the formula will sort the names alphabetically and also return the corresponding number in column C, see picture below.

Sort values by corresponding text1

The formula is in column C.

=INDEX($A$1:$A$6, MATCH(SMALL(IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), ROUND(ROW(A1)*0.5, 0)), IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), 0)+MOD(ROW(A2), 2))

You must enter this formula as an array formula. Here are the steps:

  1. Copy and paste the formula in cell C1
  2. Press and hold CTRL + SHIFT simultaneously
  3. Press Enter once
  4. Release all keys.

The formula is now surrounded by curly brackets, like this {=formula} if you did it right. Check your formula bar and make sure you have the curly brackets.

Then copy cell C1 and paste to cells beneath.

Back to top

2.1 Explaining the formula in cell C1

Step 1 - Sort data alphabetically

The COUNTIF function counts the number of cells in a cell range that meets a condition. You can use the COUNTIF function to create an array containing numbers that represent the sort order.

COUNTIF($A$1:$A$6, "<"&$A$1:$A$6)

becomes

COUNTIF({"Nick"; 10; "Zack"; 15; "John"; 13}, "<"&{"Nick"; 10; "Zack"; 15; "John"; 13})

and returns {1; 0; 2; 2; 0; 1}

The COUNTIF function compares each value in the second argument with values in the first argument and labels them with numbers depending on their position in a sorted list. The magic is done by this code "<"& in the second argument. It appends a less than sign to each value in the second argument.

Step 2 - Filter text values

The ISTEXT function returns TRUE if a cell contains a text value and FALSE if not.

ISTEXT($A$1:$A$6)

becomes

ISTEXT({"Nick"; 10; "Zack"; 15; "John"; 13})

and returns

{TRUE; FALSE; TRUE; FALSE; TRUE; FALSE}

TRUE and FALSE are boolean values. The numerical equivalents are TRUE - 1, FALSE - 0 (zero).

Step 3 - Filter text values

The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.

IF(logical_test, [value_if_true], [value_if_false])

IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), "")

becomes

IF({TRUE; FALSE; TRUE; FALSE; TRUE; FALSE}, {1; 0; 2; 2; 0; 1}, "")

and returns {1; ""; 2; ""; 0; ""}

We want to sort text values only, that is why we use the IF and ISTEXT functions to check if a value is a text value.

The double quotations "" indicate that a cell is empty.

Step 4 - Find n-th the smallest number in array

The SMALL function returns the k-th smallest value from a group of numbers.

SMALL(arrayk)

SMALL(IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), ROUND(ROW(A1)*0.5, 0))

becomes

SMALL({1;"";2;"";0;""}, ROUND(ROW(A1)*0.5, 0))

becomes

SMALL({1;"";2;"";0;""}, ROUND(0.5, 0))

becomes

SMALL({1;"";2;"";0;""}, 1)

and returns 0.

This part of the formula ROUND(ROW(A1)*0.5, 0) requires explanation, it returns a value depending on the relative cell reference A1. It changes as you copy the formula downwards. In cell C1 ROUND(ROW(A1)*0.5, 0) returns 1, C2 returns 1, C3 returns 2, C4 returns 2, C5 returns 3 and C6 returns 3. This makes it possible to get both the number and text from column A using the INDEX function, I will explain that later.

Read more about SMALL function.

Step 6 - Find the position in the array

MATCH(SMALL(IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), ROUND(ROW(A1)*0.5, 0)), IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), 0)

becomes

MATCH(0, IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), 0)

becomes

MATCH(0, {1;"";2;"";0;""}, 0)

and returns 5.

Learn more about the MATCH function.

Step 7 - Return values from column A

INDEX($A$1:$A$6, MATCH(SMALL(IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), ROUND(ROW(A1)*0.5, 0)), IF(ISTEXT($A$1:$A$6), COUNTIF($A$1:$A$6, "<"&$A$1:$A$6), ""), 0)+MOD(ROW(A2), 2))

becomes

INDEX($A$1:$A$6, 5+MOD(ROW(A2), 2))

becomes

INDEX($A$1:$A$6, 5+0)

and returns "John" from cell A5.

As you copy the formula and paste it to cells below, this part changes MOD(ROW(A2), 2). There is a relative cell reference here also, MOD(ROW(A2), 2) returns 0 in cell C1, 1 in cell C2. This makes it possible to also fetch the corresponding value.

Back to top

2. Sort items by adjacent number in every other value - Excel 365 formula

Sort values by corresponding text excel 365 dynamic array formula

Formula in cell D2:

=LET(x, INDEX(B3:B8, SEQUENCE(ROWS(B3:B8)/2, 2)), SORT(x))

Back to top

3. Sort items by adjacent number in every other value horizontally

Sort values by corresponding text2

If data is arranged horisontally, see picture above. Use this array formula in cell A3:

=INDEX($A$1:$F$1, MATCH(SMALL(IF(ISTEXT($A$1:$F$1), COUNTIF($A$1:$F$1, "<"&$A$1:$F$1), ""), ROUND(COLUMN(A1)*0.5, 0)), IF(ISTEXT($A$1:$F$1), COUNTIF($A$1:$F$1, "<"&$A$1:$F$1), ""), 0)+MOD(COLUMN(B1), 2))

Back to top


Back to top