Core Idea
A list of maps is essentially a table. It has the headers (keys) and its respective values. Here’s a cool Deluge function that allows you to sort your dataset by any of the map keys as if you’re sorting columns in Excel. Because this functionality doesn’t exist in Deluge natively yet, this can be achieved with some string manipulation, additions and by reassigning indexes.
Example Dataset
Let’s use the table below as an example.Product | Date of Sale | Quantity Sold |
---|---|---|
Orange | 2020-04-08 | 20 |
Apple | 2020-02-28 | 15 |
Kiwi | 2020-05-15 | 50 |
Peach | 2020-11-22 | 35 |
Strawberry | 2020-12-24 | 10 |
Watermelon | 2021-01-11 | 25 |
Below is the JSON string for the table you may copy for testing
[
{
"Products": "Orange",
"Date_of_Sale": "2020-04-18",
"Quantity_Sold": 20
},
{
"Products": "Apple",
"Date_of_Sale": "2020-02-28",
"Quantity_Sold": 15
},
{
"Products": "Kiwi",
"Date_of_Sale": "2020-05-15",
"Quantity_Sold": 50
},
{
"Products": "Peach",
"Date_of_Sale": "2020-11-22",
"Quantity_Sold": 35
},
{
"Products": "Strawberry",
"Date_of_Sale": "2020-12-24",
"Quantity_Sold": 10
},
{
"Products": "Watermelon",
"Date_of_Sale": "2021-01-11",
"Quantity_Sold": 25
}
]
Select the Key for Sort, Add a Suffix to the Key, then Add to a New List
- unsortedList is the original list that you want to sort
- Configure the sortKey – this will be key that you wish to sort your list of maps by
In this example, we’ve used “Quantity_Sold”, but you can also use “Products” or “Date_of_Sale”) - Compile the all sortKey values in keyList
- Sort keyList – true for ascending order (default), false for descending order
- Iterate over unsortedList in order the sorted keyList to create the sortedList
unsortedList = [{"Products":"Orange","Date_of_Sale":"2020-04-18","Quantity_Sold":20},{"Products":"Apple","Date_of_Sale":"2020-02-28","Quantity_Sold":15},{"Products":"Kiwi","Date_of_Sale":"2020-05-15","Quantity_Sold":50},{"Products":"Peach","Date_of_Sale":"2020-11-22","Quantity_Sold":35},{"Products":"Strawberry","Date_of_Sale":"2020-12-24","Quantity_Sold":10},{"Products":"Watermelon","Date_of_Sale":"2021-01-11","Quantity_Sold":25}];
// Configure sortKey (the key in which you would like to sort your list by)
sortKey = "Quantity_Sold";
// Compile sortKey values into keyList so you can sort it
keyList = List();
for each u in unsortedList
{
keyList.add(u.get(sortKey));
}
// Sort keyList (false for descending order)
keyList = keyList.distinct().sort(false);
// Iterate over unsortedList in order of the sorted keyList to create the sortedList
sortedList = list();
for each k in keyList
{
for each u in unsortedList
{
if(u.get(sortKey) == k)
{
sortedList.add(u);
}
}
}
info sortedList;
info "sortedList : " + sortedList;
Click here to copy these scripts. For more Zoho-wizardry, check out our GitHub page.
Contact Us!
Book a free 30-minutes consultation with a Zoho expert or send us an email

Related Resources
This set up allows you to insert subform rows and add to picklist dynamically with...
Deluge script for sorting lists containing maps by the specific key (by date-time/alphabetical...
When scripting, ask yourself this question - will this value ever be null? If it's...
This script allows you to create a HTML table in Deluge with dynamic rows, then merge...
When a Zoho CRM record is created/updated/deleted via Deluge, the system does not...
Click Here to Apply
Description
Location
Bastrop/Smithville area
Role
Entry...
Click Here to Apply
Description
Location
New Braunfels area
Role
The Partner...
This tutorial will illustrate how to create a custom function (using Deluge scripting)...
This tutorial demonstrates the integration between Shopify and Zoho Inventory. Use...
This tutorial provides a Creator script that loads related records from Zoho CRM...