## Sort records based on two columns

**Ralee asks in in this blog post: Sort values in parallel (array formula)**

If there is information in adjacent columns, say:

Joel, AA, 7

Mark, FF, 31

Nick, AA, 7

with the possibility of matching sort criteria (columns 2 & 3), how would I display the corresponding data after the sort (sorting only columns 2 & 3) without repeats; that is,

Joel, AA, 7

Nick, AA, 7

Mark, FF, 31

Instead of:

Joel, AA, 7

Joel, AA, 7

Mark, FF, 31

?

**Answer:**

**Array formula in B9:**

To enter an array formula, type the formula in a cell then press and hold CTRL + SHIFT simultaneously, now press Enter once. Release all keys.

The formula bar now shows the formula with a beginning and ending curly bracket telling you that you entered the formula successfully. Don't enter the curly brackets yourself.

**Array formula in C9:**

**Array formula in D9:**

**Explaining array formula in cell B9**

**Step 1 - Create array containing numbers representing rank order if sorted**

The COUNTIF function lets you count values based on a condition. The less than sign lets you compare values based on sort order.

COUNTIF($C$3:$C$5, "<"&$C$3:$C$5)

becomes

COUNTIF({" AA";" FF";" AA"},{"< AA";"< FF";"< AA"})

and returns

{0;2;0}

**Step 2 - Create rank order based on second column**

Integers represent the sort order of column C and decimal values represent the sort order of column D, combining them creates an array that lets you identify the sort order based on both columns.

1/(COUNTIF($D$3:$D$5, ">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)

becomes

1/({0;2;0}+1+ROW($D$3:$D$5)/65536)

becomes

1/({0;2;0}+1+{3;4;5}/65536)

becomes

1/({0;2;0}+1+{0.0000457763671875;0.00006103515625;0.0000762939453125})

becomes

1/({1.00004577636718;3.00006103515625;1.00007629394531})

and returns

{0.9999542257282;0.333326551787276;0.999923711875012}

**Step 3 - Add arrays**

COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536))

becomes

{0;2;0} + {0.9999542257282;0.333326551787276;0.999923711875012}

and returns

{0.9999542257282;2.33332655178728;0.999923711875012}

**Step 4 - Extract k-th smallest value in array**

To be able to return a new value in a cell each I use the SMALL function to filter column numbers from smallest to largest.

The ROWS function keeps track of the numbers based on an expanding cell reference. It will expand as the formula is copied to the cells below.

SMALL(COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)),ROWS($A$1:A1))

becomes

SMALL({0.9999542257282;2.33332655178728;0.999923711875012},ROWS($A$1:A1))

becomes

SMALL({0.9999542257282;2.33332655178728;0.999923711875012},1)

and returns

0.999923711875012.

**Step 5 - Find position of value in array**

The MATCH function finds the relative position in a cell range or array.

MATCH(SMALL(COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)),ROWS($A$1:A1)),COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)),0)

becomes

MATCH(0.999923711875012,COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)),0)

becomes

MATCH(0.999923711875012, {0.9999542257282;2.33332655178728;0.999923711875012},0)

and retrurns 3.

**Step 6 - Return corresponding value based on position**

The INDEX function returns a value based on a cell reference and column/row numbers.

INDEX($B$3:$B$5,MATCH(SMALL(COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)),ROWS($A$1:A1)),COUNTIF($C$3:$C$5,"<"&$C$3:$C$5)+(1/(COUNTIF($D$3:$D$5,">"&$D$3:$D$5)+1+ROW($D$3:$D$5)/65536)),0))

becomes

INDEX($B$3:$B$5,3)

and returns "Nick in cell B9.

### Sort values category

Table of Contents Sort a column - Excel 365 Sort a column using array formula Two columns sorting by the […]

The image above demonstrates a formula in cell D3 that extracts unique distinct numbers and text values sorted from A […]

Array formula in D5: =SMALL(IF(($B$3:$B$12<=$E$3)*($B$3:$B$12>=$E$2), $B$3:$B$12, "A"), ROWS($A$1:A1)) How to create an array formula Copy array formula Select cell D5 […]

This article demonstrates a formula that extracts values based on a condition and sorts the returned values based on values […]

The image above shows a table with two columns in cell range B3:C16, it contains random text values in column […]

This article demonstrates a formula that sorts cell values by their frequency, in other words, how many times a value […]

Table of Contents Sort text from two columns combined (array formula) How to create an array formula How to copy […]

This article demonstrates a macro and a formula that allows you to sort delimited data in a cell or cell […]

This article demonstrates a formula that sorts items arranged horizontally based on the adjacent numbers, every other column contains a […]

This article demonstrates how to sort a specific column in an Excel defined Table based on event code. The event […]

Question: How do I sort a range alphabetically using excel array formula? Answer: Cell range $B$2:$E$5 contains text values in random […]

The formula in cell B8 extracts a list sorted based on frequency. Array formula in B8: =TEXTJOIN("", TRUE, IF(MIN(IF((MAX(IF(COUNTIF($B$7:B7, $B$2:$E$5)=0, […]

Overview The array formula in cell range C2:C6 creates a random list from the values in cell range A2:A6. Array […]

In this article, I will demonstrate two techniques for counting per row. The first example is simple and straightforward. The […]

The image above demonstrates a formula in cell D3 that sorts values based on character length, the value with the […]

The following array formula extracts duplicate values sorted from A to Z from cell range B3:B21. Excel array formula in […]

I will in this article demonstrate three different techniques to sort a data set in Excel. The first method sorts […]

This article describes a formula that sorts values arranged in a column from A to z by every other value. […]

This article demonstrates how to distribute values into specific ranges with possible overlapping ranges. I have written articles about filter […]

## Functions in this article

More than 1300 Excel formulas

## Excel formula categories

## Excel categories

### 4 Responses to “Sort records based on two columns”

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

Great! That's exactly what I needed.

~Thanks

there is an error is your named ranges

Qty (B2:B15) should be Qty (d2:d15)

Descr (B2:B15) should be Descr (c2:c15)

Paul,

thanks!

Please tell me why you put "65536" in your array formula

thx