Today, we want to learn how the SORT func­tion works in Excel. 

Excel SORT Function

The SORT func­tion is avail­able from Dynam­ic Array Excel, which is around 2021. Let’s start with the sim­plest ver­sion of the SORT func­tion, where we only need to write an array. We have to remem­ber that the array must be giv­en with­out head­ers. Let’s select our data and put it as an argu­ment and let’s put our for­mu­la into a cell (Fig. 1)

=SORT(A2:C169)

SORT function
Fig. 1 SORT function

And we have the results. The most impor­tant thing about for­mu­las in Dynam­ic Array Excel is that they spill. It means that our for­mu­la is in only one cell, but the results spill. The for­mu­la is only in cell E2, but when we click on cell E3, we can see that the for­mu­la is grayed out in the for­mu­la bar. It means that it con­tains results of this func­tion, but does­n’t con­tain the func­tion itself (Fig. 2)

Formula grayed out
Fig. 2 For­mu­la grayed out

When we add an array with data to be sort­ed, Excel’s sort­ing will be based on the first col­umn, which is the col­umn with dates. In the sort­ed array we don’t see dates but num­bers because Excel does­n’t know how to copy the for­mat­ting in array for­mu­las. If we want to have prop­er for­mat­ting, we can copy the for­mat­ting using the For­mat Painter (2) from the Home tab (1). We will also high­light the cell with the for­mu­la (3) (Fig. 3)

Using the Format Painter command
Fig. 3 Using the For­mat Painter command

Now, our results are for­mat­ted. Since we sort­ed by the first col­umn, our results are basi­cal­ly the same as in our orig­i­nal col­umn. To change that, we can add the sec­ond argu­ment to our SORT func­tion, which is the sort index. It’s the num­ber of a col­umn by which we want to sort our data. Let’s write 2, mean­ing the sec­ond col­umn (Fig. 4)

=SORT(A2:C169,2)

SORT formula with the second argument
Fig. 4 SORT for­mu­la with the sec­ond argument

We can see that the sort­ing has changed. Now, we can see that only Chan­dlers are at the top because it’s an ascend­ing sort­ing. This way we can sort by any col­umn. Let’s move on to the third argu­ment, which is the sort order. Let’s change it to a descend­ing sort­ing by writ­ing ‑1 (Fig. 5)

=SORT(A2:C169,2,-1)

Descending sorting
Fig. 5 Descend­ing sorting

Now, the first sales­man in Ross. Let’s add some­thing more. Let’s sort by Sales from the largest to the small­est. Now, the sales are writ­ten ran­dom­ly in the Sales col­umn. We can add the third col­umn to the sec­ond argu­ment. We can also add a ref­er­ence to cells where num­bers of columns are writ­ten. It’s very impor­tant to write them in the cor­rect order. In our case the first cell from the top con­tains 2 and the sec­ond one con­tains 3. It means that first, we will sort by the sec­ond col­umn, and then by the third col­umn (Fig. 6)

Adding a cell reference
Fig. 6 Adding a cell reference

And we have the results. We can see that Ross is sort­ed by val­ues in the Sales col­umn. The same is with the rest of the sales­men. Now, let’s focus on the third argu­ment once again. We have two columns of sort­ing in the sec­ond argu­ment, but only one num­ber (-1) in the third argu­ment which is the sort order. It means that the sort­ing is descend­ing for each col­umn. When we want to have an ascend­ing order for one col­umn and a descend­ing order for anoth­er col­umn, we can do the same as we did with the sec­ond argu­ment. We have to refer to some cells in Excel. In our case we have a sort­ing order for each col­umn (Fig. 7)

Referring to other cells
Fig. 7 Refer­ring to oth­er cells

Now, we have an ascend­ing sort­ing for the Sales­man col­umn, and a descend­ing sort­ing for the Sales column. 

When we don’t want any addi­tion­al cells in our sheet, we can hard code them in the for­mu­la by press­ing F9 key. It changes the argu­ment into an array. We can do the same with the sort order argu­ment. When we look at the for­mu­la now, we can see that the sec­ond col­umn in ascend­ing, and the third col­umn is descend­ing (Fig. 8)

Hard coding the values
Fig. 8 Hard cod­ing the values

Now that we have every­thing we need­ed, we can can­cel the unnec­es­sary cells. We don’t even need the third argu­ment, as it is reserved for hor­i­zon­tal sort­ing. Here, we are sort­ing only by columns, so we can leave it like that. Here are our results (Fig. 9)

Results
Fig. 9 Results

https://www.youtube.com/watch?v=RzxTurQaF5I&t