top of page
  • Writer's pictureHugh Marshall

Sorting a List of JSON Maps by a Specific Field using Zoho Deluge

Updated: Aug 22, 2023

When you use custom functions in Zoho CRM, you will often find yourself dealing with lists of JSON maps. One thing that you cannot do natively with deluge is sort a list of JSON maps by a specific field, but you can sort a list of values. We can use this to our advantage.


Let's take this list of tasks that we have here in JSON format, as well as the data represented as a table.

We can see that the data is currently sorted by the 'id', but we want to sort it using the 'due date' with the oldest at the top. For us, that is a much more realistic way to view our upcoming and overdue tasks.


Example Code

Using this snippet of code, we can achieve what we are after. I'll explain in detail all the components that make it work.

How it works

First, we iterate through all of the tasks in the list and retrieve the value we want to sort by, which in this case is the 'Due_Date.' We save each of these values to another list called 'unsortedKeys'. In situations where the value is not unique and there's a possibility of having the same value for multiple records, I like to append the record's ID to the end of the value. This is done as a precautionary measure, as the sorting process will not work if there are any duplicate keys.

Simultaneously, while iterating through the tasks, we also create a new Map called 'unsortedKeyValueData'. In this Map, we use the sorting value as the key and the task data as the corresponding value. This mapping allows us to easily locate a task based on its unique key value.

Sorting the Keys

Since the 'unsortedKeys' list only contains values, we can now sort these values using the deluge sort function. By doing so, we can arrange the keys in ascending order. In the case of dates, this means the oldest date will appear first.


Reordering the List

Now that we have a sorted list of keys, all we need to do is iterate through this list. During each iteration, we retrieve the corresponding values from the 'unsortedKeyValueData' map we created earlier. We can then add these values to a new list which will be in the order we want.


Here is an example of the result we achieved.

By following the steps outlined above, you can effectively sort any list of JSON Maps by a specific field. This is really useful especially when you are building custom related lists or reordering subform records. Happy sorting!


Need Help? Contact us!


Resources


208 views

Recent Posts

See All

コメント


Looking for further Assistance

If you are looking for further assistance with Zoho applications feel free to contact us to see what we can do for you.

bottom of page