Sorting Lists Containing Maps with Deluge

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.
ProductDate of SaleQuantity Sold
Orange2020-04-0820
Apple2020-02-2815
Kiwi2020-05-1550
Peach2020-11-2235
Strawberry2020-12-2410
Watermelon2021-01-1125

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