Skip to content

sort_values_desc

sort_values_desc(arr)

Description

Return sorted array (descending)

Usage

Call or Deploy sort_values_desc ?
Call sort_values_desc directly

The easiest way to use bigfunctions

  • sort_values_desc function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy sort_values_desc in your project

Why deploy?

  • You may prefer to deploy sort_values_desc in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

sort_values_desc function can be deployed with:

pip install bigfunctions
bigfun get sort_values_desc
bigfun deploy sort_values_desc

Examples

select bigfunctions.eu.sort_values_desc([1, 4, 3])
select bigfunctions.us.sort_values_desc([1, 4, 3])
select bigfunctions.europe_west1.sort_values_desc([1, 4, 3])
+--------------+
| sorted_array |
+--------------+
| [4, 3, 1]    |
+--------------+

Use cases

You have a table of product sales with columns like product_id and sales_amount. You want to find the top 3 products by sales in descending order. You can use sort_values_desc within an aggregation to achieve this:

SELECT product_id
FROM `your_project.your_dataset.your_sales_table`
GROUP BY product_id
ORDER BY bigfunctions.YOUR_REGION.sort_values_desc(ARRAY_AGG(sales_amount)) DESC
LIMIT 3

Explanation:

  1. ARRAY_AGG(sales_amount): For each product_id, this gathers all the sales_amount values into an array.
  2. sort_values_desc(...): This sorts the array of sales amounts in descending order. The highest sales amount will now be the first element in each array.
  3. ORDER BY ... DESC: This orders the product_id groups based on the sorted sales amount arrays in descending order. Since the largest sales amount is the first element of each array after sorting, ordering by the array itself (descending) effectively orders by the highest sales amount.
  4. LIMIT 3: This returns only the top 3 product_ids based on the ordering.

Another Use Case (Data Cleaning):

Imagine you have a table with a column containing lists of dates (perhaps representing important events related to a customer). These date lists might be in any order. You want to consistently store these dates in descending chronological order. You could use sort_values_desc:

SELECT
    customer_id,
    bigfunctions.YOUR_REGION.sort_values_desc(dates_array) AS sorted_dates
FROM
    `your_project.your_dataset.your_customer_table`

This would update or create a new column sorted_dates with the dates arranged from most recent to oldest.

Remember to replace YOUR_REGION with the appropriate BigQuery region (e.g., us, eu, asia-northeast1, etc.) that corresponds to your dataset's location.


Need help or Found a bug?
Get help using sort_values_desc

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about sort_values_desc

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on