bigfunctions > sort_values_desc
sort_values_desc¶
Call or Deploy sort_values_desc
?
✅ You can call this sort_values_desc
bigfunction directly from your Google Cloud Project (no install required).
- This
sort_values_desc
function is deployed inbigfunctions
GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error). - Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework
Public BigFunctions Datasets:
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Description¶
Signature
sort_values_desc(arr)
Description
Return sorted array (descending)
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] |
+--------------+
Need help using sort_values_desc
?
The community can help! Engage the conversation on Slack
For professional suppport, don't hesitate to chat with us.
Found a bug using 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.
For professional suppport, don't hesitate to chat with us.
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:
ARRAY_AGG(sales_amount)
: For eachproduct_id
, this gathers all thesales_amount
values into an array.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.ORDER BY ... DESC
: This orders theproduct_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.LIMIT 3
: This returns only the top 3product_id
s 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.
Spread the word¶
BigFunctions is fully open-source. Help make it a success by spreading the word!