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. This is 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

Select the Key for Sort, Add a Suffix to the Key, then Add to a New List

  • listvar is your list variable. To ensure that it is read as a list, use a toList() function.
  • Create a new list (newList).
  • Then, set the variable n as “000” (this will acccount for a list of up to 1000 elements).
  • Loop over the list, get() the key that you want to sort by and add n as the suffix.
    • To sort by Products (we’ll use this for this demo)
      • sortkey = l.get("Products") + n;
    • To sort by Date of Sale
      • sortkey = l.get("Date_of_Sale").replaceAll("-","") + n;
    • To sort by Quantity Sold
      • sortkey = l.get("Quantity_Sold") + n;
  • Convert n to a number, add 1, then convert it back to a string.
  • If the length of n is one digit, we wanna add only two zeros at the front, if it’s two, we should only add one zero. If it’s three, zero does not need to be added.
  • At the end of the loop, add sortkey to the newList variable (this list will contain only the selected key’s values with the added suffix).
				
					originalList = listvar.toList();
n = "000";
newList = List();
for each  l in originalList
{
	sortkey = l.get("Products") + n;
	n = n.toLong() + 1;
	n = n.toString();
	if(n.length() = 1)
	{
		n = "00" + n;
	}
	else if(n.length() = 2)
	{
		n = "0" + n;
	}
	newList.add(sortkey);
}
				
			

Sort the New List and Get the Indexes

  • Use the sort() function to sort the values newList by ascending (true) or descending (false) order.
  • Create another list (this will be used to store the end result of the sorted list).
  • Loop over variable newList and extract the index of each element by getting the numbers without the zero(s).
  • At this point, you have the sorted list, and the respective index that acts as an “id” for each of the line items.
  • All you gotta now is to create a new map by using get(index)function on the original list, then add the map to the new sortedList.
  • By the end of the loop iteration, sortedList will contain the sorted output of the list.
				
					newList.sort(true);
sortedlist = List();
for each  n in newList
{
	if(n.right(3).left(2) = "00")
	{
		index = n.right(1);
	}
	else if(n.right(3).left(1) = "0" && n.right(3).left(2).right(1) != "0")
	{
		index = n.right(2);
	}
	else
	{
		index = n.right(3);
	}
	map = originalList.get(index);
	sortedlist.add(map);
}
info 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