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.
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
}
]
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.
Book a free 30-minutes consultation with a Zoho expert or send us an email