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

Table of Contents Sort a column using array formula Two columns sorting by the second column Sort alphanumeric values I [โฆ]

Create a unique distinct sorted list containing both numbers text removing blanks

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

Sort dates within a date range

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 [โฆ]

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

Sort column based on frequency

Question: How do I create a new unique distinct list from a column. I also want the list sorted from large [โฆ]

Lookup and return multiple values sorted in a custom order

Pat asks: Hi Oscar, Thanks for creating such a helpful website and I've a question if I would like to [โฆ]

Sort text cells alphabetically from two columns

Table of Contents Sort text from two columns combined (array formula) Sort text from multiple cell ranges combined (user defined [โฆ]

Sort values in an Excel table programmatically [VBA]

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

Denisa asks: I have a problem and i cant figure it out, even if i'm seraching for 2 days. I [โฆ]

Sort values in a cell based on a delimiting character [VBA]

This article demonstrates a macro that allows you to sort delimited data in a cell or cell range from A [โฆ]

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