## Sort based on frequency and criteria

**Andre asks:**I am trying to list people with the highest scores based on certain criteria.

My data:

column

A B C D

Mike 207 Yes Life

Greg 207 Yes Life

Sid 207 Yes Life

Greg 207 Yes Life

Greg 207 Yes Life

Sid 207 Yes Life

Greg 207 No Life

Sid 204 No Health

criteria

countif b= 207 and column c= yes and column d= Life

and it then needs to arrange it from the highest to the lowest

(Large)

and then match it with the name eg, Greg Sid or Mike

so what I am looking for is eg.

Greg 3

Sid 2

Mike 1

but it has to be in one formula.

**Answer:**

Array formula in B15:

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.

Copy cell F2 and paste it down.

Formula in C15:

Copy cell C15 and paste it down as far as needed. Try changing criteria in cell range C2:E2.

You can do this in a pivot table as well:List people with the highest scores based on criteria in a pivot table (Excel 2007)*)*

### Explaining formula in cell B15

#### Step 1 - Apply critera - AND logic

The COUNTIF function counts values based on a condition, we need three different COUNTIF functions in order to check all three critera to corresponding cell ranges.

COUNTIF($C$2,$C$5:$C$12)*COUNTIF($D$2,$D$5:$D$12)*COUNTIF($E$2,$E$5:$E$12)

becomes

COUNTIF(207,{207; 207; 207; 207; 207; 207; 207; 204})*COUNTIF("Yes",{"Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "No"; "No"})*COUNTIF("Life",{"Life"; "Life"; "Life"; "Life"; "Life"; "Life"; "Life"; "Health"})

becomes

{1; 1; 1; 1; 1; 1; 1; 0}*{1; 1; 1; 1; 1; 1; 0; 0}*{1; 1; 1; 1; 1; 1; 1; 0}

and returns

{1;1;1;1;1;1;0;0}

#### Step 2 - Filter values meeting criteria

The IF function has three arguments, the first one must be a logical expression. If the expression evaluates to TRUE then one thing happens (argument 2) and if FALSE another thing happens (argument 3). The following lines explain the logical expression:

IF(COUNTIF($C$2, $C$5:$C$12)* COUNTIF($D$2, $D$5:$D$12)* COUNTIF($E$2, $E$5:$E$12),(COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), "")

becomes

IF({1; 1; 1; 1; 1; 1; 0; 0},(COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), "")

The COUNTIF function in the second argument has a < less than sign concatenated to all values in the second argument, this makes the COUNTIF function return an array containing numbers representing the rank order if the list were sorted. This is needed to create unique numbers for each value because the FREQUENCY function can't handle text values only numerical values.

IF({1; 1; 1; 1; 1; 1; 0; 0},(COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), "")

becomes

IF({1; 1; 1; 1; 1; 1; 0; 0}, {5; 1; 6; 1; 1; 6; 1; 6}, "")

and returns

{5; 1; 6; 1; 1; 6; ""; ""}

#### Step 3 - Calculate frequency

The FREQUENCY function calculates how often values occur within a range of values and then returns a vertical array of numbers.

FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1)

becomes

FREQUENCY({5; 1; 6; 1; 1; 6; ""; ""}, {5; 1; 6; 1; 1; 6; 1; 6})

and returns

{1; 3; 2; 0; 0; 0; 0; 0; 0}.

#### Step 4 - Extract k-th largest number

The LARGE function returns the k-th largest number in a cell range or array. LARGE( array, k)

LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1))

becomes

LARGE({1; 3; 2; 0; 0; 0; 0; 0; 0}, ROWS($A$1:A1))

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

LARGE({1; 3; 2; 0; 0; 0; 0; 0; 0}, ROWS($A$1:A1))

becomes

LARGE({1; 3; 2; 0; 0; 0; 0; 0; 0}, 1)

and returns 3.

#### Step 5 - Find the relative position of the value in the array

The MATCH function returns a number representing the position of a given value in an array or cell range.

MATCH(LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1)), FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0)

becomes

MATCH(3, FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0)

becomes

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

and returns 2.

#### Step 6 - Return value

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

INDEX($B$5:$B$12, MATCH(LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1)), FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0))

becomes

INDEX($B$5:$B$12, 2)

and returns "Greg" in cell B15.

#### Step 7 - Trap errors

The formula returns errors when all values have been displayed, the IFERROR function converts the errors into a given value, in this case "" (nothing).

IFERROR(INDEX($B$5:$B$12, MATCH(LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1)), FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0)), "")

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

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

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

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

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

Sort a range from A to Z [Array formula]

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

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