Sort Function In Google Sheets

Sort Function In Google Sheets
Reading Time: 5 minutes

Master the SORT Function in Google Sheets

Learn how to use the Google Sheets SORT function and examples of its variations.

What is SORT Function in Google Sheets

While analyzing your data in Google Sheets, you may need to rearrange data based on a specific dimension – for e.g. arranging customer names alphabetically; arranging revenue in a high-to-low descending order. You may also need a combination of such ascending-descending rules on more than one column. SORT function in Google Sheets can help you with this.

Syntax of SORT Function in Google Sheets

=SORT(range, sort_column, is_ascending, [sort_column2, is_ascending2, …])
  1. range:- the data range/group of cells used for the sorting.
  2. sort_column:- the column that the data will be sorted by. There are two ways in which you can specify the value in the sort_column parameter :
    1. Specify the number of the column in the range used for sorting (for eg. 2).
    2. A reference of a column within the range used for sorting (for eg. B2:B20).
  3. is_ascending:- Specify True if you want the sorting to be done in ascending order, False if you want the sorting to be done in descending order.
  4. sort_column2:- use this parameter if you want to sort the data with multiple columns. Specify the number of the column or reference of a column within the range, as done in parameter 2.
  5. is_ascending2:- similar to parameter 3. Use this only if you want to sort the data range with multiple columns. This parameter will be implemented on sort_column2.

Naming the data range

  1. Selecting the data range:
    • We will use a dataset for querying. For ease of querying, we will name the range so we do not need to select the data range every time.
    • Click Ctrl + A (Windows) or Cmd+A (Mac) to select the entire data range.
    • Go to Data
    • Select Named Ranges
Selecting dataset for creating a named range
Figure 1: Selecting dataset for creating a named range
  1. Naming the range:
    • Give a suitable name to your data range. Here I am giving it a Dataset.
    • Click on Done to complete the process of naming.
Going to Named Ranges to name our data range
Figure 2: Going to Named Ranges to name our data range

Sorting using one column in ascending order (low-to-high)  

  • To sort the data range by a single column in ascending order, use the syntax :
Syntax: =SORT(range, column_index, ascending_order)
  • To sort the dataset by a specific column, using the syntax, use :
=SORT(Dataset,1,TRUE)
  • Here, I have used the named range Dataset as the range to sort.
  • I have used column number 1, i.e. Name to sort the dataset.
  • I have kept order as Ascending. Therefore, the output is:
Sorting the dataset using one column in ascending order
Figure 3: Sorting the dataset using one column in ascending order using the SORT function in Google Sheets

Sorting using one column in descending order (high-to-low)

  • To sort the data range by a single column in descending order, use the syntax :
Syntax: =SORT(range, column_index, ascending_order)
  • To sort the dataset by a specific column, using the syntax, use :
=SORT(Dataset,1,FALSE)
  • Here, I have used the named range Dataset as the range to sort.
  • I have used column number 1, i.e. Name to sort the dataset.
  • I have kept order as Descending. Therefore, the output is:
Sorting the dataset using one column in descending order
Figure 4: Sorting the dataset using one column in descending order using the SORT function in Google Sheets

Sorting using multiple columns

  • To sort the data range by multiple columns, use the syntax :
Syntax: =SORT(range, column_index1, ascending_order1, column_index2, ascending_order2)
  • To sort the dataset by multiple columns, using the syntax, use :
=SORT(Dataset,2,TRUE,3,FALSE)
  • Here, I have used the named range Dataset as the range to sort.
  • I have used column number 2, i.e. Quarter to sort the dataset in ascending order. In the case of multiple columns, the first column gets the priority for sorting.
  • If the Quarter values are the same, the dataset will be sorted using column number 3, i.e. Sales to sort the dataset in descending order, as shown below:
Example of multiple sorts in different orders
Figure 5: Example of SORT function in Google Sheets where multiple sorts are done in different orders

Sorting with Column References

  • To sort the data range by a column reference, use the syntax :
Syntax: =SORT(range, column_reference, ascending_order)
  • Here, I have used :
=SORT(A2:D20,B2:B20,1)
  • Here, I have used the data between cells A2 to D20 as the range to sort.
  • I have used column reference as B2:B20 to sort the data.
  • Note: The number of columns and rows returned by the SORT function should be the same as the input range, therefore, the column reference also will change if we change the range.
  • I have kept order as Ascending. Therefore, the output is:
Example of sorting using column reference for a specific number of cells of a dataset
Figure 6: Example of SORT function in Google Sheets where column references are used for a specific number of cells of a dataset

Example 5: Horizontal Sorting using Google Sheets Sort function

  • To sort the data range horizontally, use the syntax :
Syntax: =transpose(SORT (transpose (range, column_number, ascending_order)
  • Here I have used:
=transpose (SORT (transpose (Dataset), 1, true)
  • The inner transpose is used to transpose the entire dataset, i.e. the columns are replaced by rows and vice versa.
  • They are then sorted using the SORT function.
  • The outer transpose is used to transpose the sorted dataset back to the initial view, as shown below:
Example of sorting the dataset horizontally using transpose function
Figure 7: Example of sort function in Google Sheets where the dataset is sorted horizontally using transpose function

Conclusion

The SORT function in Google Sheets can be used to sort data as per convenience. Some of the most frequently used applications were discussed in the article. If you want to know more about SORT function in Google Sheets, go through the official documentation here.

See Also

You can checkout other equally good articles on Google Sheets here.

How to Create Google Calendar Events from Google Sheets: Learn how to create Google Calendar events from Google Sheets.

ARRAYFORMULA in Google Sheets: Learn how to use the ARRAYFORMULA function in Google Sheets and its variations.

Delete Empty rows in Google Sheets: In this article we would see how to delete empty rows in Google Sheets using two different ways.

How to group rows in Google Sheets: We will learn how to group rows in Google Sheets. We will also have a look at nested grouping.