For Zoho services only


I'm currently part of a wider delivery team at Ascent Business Solutions, recognised as a leading Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions support organisations with everything from targeted technical fixes through to full Zoho CRM implementations and long-term platform adoption. Working as a team rather than a one-person consultancy allows projects to move forward consistently, with access to the right skills at each stage.

The team I manage specialises in API integrations between Zoho and third-party finance and commerce platforms such as Xero, Shopify, WooCommerce, and eBay. Much of our work involves solving integration challenges that fall outside standard documentation, supporting new ideas, new sectors, and evolving business models.

Success is measured through practical outcomes and return on investment, ranging from scaling small operations into high-turnover businesses to delivering rapid gains through online payments, automation, and streamlined digital workflows.

If you are looking for structured Zoho expertise backed by an established consultancy, you can contact Ascent Business Solutions on 0121 392 8140 (UK), email info@ascentbusiness.co.uk, or visit https://www.ascentbusiness.co.uk.
Zoho Deluge: Sort a Map by a specific field

Zoho Deluge: Sort a Map by a specific field

What?
This is a quick article to template some code to me on sorting a map variable in Zoho Deluge, specifically Zoho Creator.

Why?
I do this a lot but in the following example, I want to sort a list of records by their date in descending order. Yes this functionality exists in reports but I want to do it in code so as to display it in a HTML table.

What I have:
copyraw
id      name        date
---------------------------------------------
46498   Joel        1977-11-14T12:30:00+00:00
8949    Anonymouse  1923-10-16T15:00:00+00:00
335448  Amazing     1997-07-05T23:15:00+00:00
  1.  id      name        date 
  2.  --------------------------------------------- 
  3.  46498   Joel        1977-11-14T12:30:00+00:00 
  4.  8949    Anonymouse  1923-10-16T15:00:00+00:00 
  5.  335448  Amazing     1997-07-05T23:15:00+00:00 
What I want:
copyraw
id      name        date
---------------------------------------------
335448  Amazing     1997-07-05T23:15:00+00:00
46498   Joel        1977-11-14T12:30:00+00:00
8949    Anonymouse  1923-10-16T15:00:00+00:00
  1.  id      name        date 
  2.  --------------------------------------------- 
  3.  335448  Amazing     1997-07-05T23:15:00+00:00 
  4.  46498   Joel        1977-11-14T12:30:00+00:00 
  5.  8949    Anonymouse  1923-10-16T15:00:00+00:00 

How?
So to give you a gist of what the following snippet of code is doing, let me give an overview:
  1. Build up a map with sample data (you won't need to this, use your own data, this one is for this demonstration only)
  2. Initialize a list to hold the keys which we will use to sort in ascending/descending order and a map created to hold all the records.
  3. Loop through the sample map to create the new map we can sort as well as to add keys to the list declared previously.
  4. Sort the list of keys in ascending/descending order.
  5. Loop through the list of keys, retrieving each record
copyraw
//
// build up sample map in random order
l_MyListUnsorted = List:Map();
m_Record = Map();
m_Record.put("name","Joel");
m_Record.put("id",46498);
m_Record.put("date","1977-11-14T12:30:00+00:00");
l_MyListUnsorted.add(m_Record);
m_Record = Map();
m_Record.put("name","Anonymouse");
m_Record.put("id",8949);
m_Record.put("date","1923-10-16T15:00:00+00:00");
l_MyListUnsorted.add(m_Record);
m_Record = Map();
m_Record.put("name","Amazing");
m_Record.put("id",335448);
m_Record.put("date","1997-07-05T23:15:00+00:00");
l_MyListUnsorted.add(m_Record);
//
// init
l_SortingKeys = List();
m_UnsortedRecords = Map();
//
for each r_Record in l_MyListUnsorted
{
	// field we want to sort by
	v_SortingKey = ifnull(r_Record.get("date"),"");
	if(!isNull(v_SortingKey))
	{
		// as this is a date from CRM (atomic date format), arrange so it can be sorted alphabetically in reverse order
		v_DatePart = v_SortingKey.getPrefix("T").toDate().toString("yyyy-MM-dd");
		v_TimePart = v_SortingKey.getSuffix("T").getPrefix("+").subString(0,5);
		v_SqlDateTime = v_DatePart + " " + v_TimePart;
		l_SortingKeys.add(v_SqlDateTime);
		m_UnsortedRecords.put(v_SqlDateTime, r_Record);
	}
}
//
// now sort list in descending order and action each record
l_SortedKeysDesc = l_SortingKeys.sort(false);
for each v_SortKey in l_SortedKeysDesc
{
	m_ThisRecord = m_UnsortedRecords.get(v_SortKey);
	info m_ThisRecord;
}
  1.  // 
  2.  // build up sample map in random order 
  3.  l_MyListUnsorted = List:Map()
  4.  m_Record = Map()
  5.  m_Record.put("name","Joel")
  6.  m_Record.put("id",46498)
  7.  m_Record.put("date","1977-11-14T12:30:00+00:00")
  8.  l_MyListUnsorted.add(m_Record)
  9.  m_Record = Map()
  10.  m_Record.put("name","Anonymouse")
  11.  m_Record.put("id",8949)
  12.  m_Record.put("date","1923-10-16T15:00:00+00:00")
  13.  l_MyListUnsorted.add(m_Record)
  14.  m_Record = Map()
  15.  m_Record.put("name","Amazing")
  16.  m_Record.put("id",335448)
  17.  m_Record.put("date","1997-07-05T23:15:00+00:00")
  18.  l_MyListUnsorted.add(m_Record)
  19.  // 
  20.  // init 
  21.  l_SortingKeys = List()
  22.  m_UnsortedRecords = Map()
  23.  // 
  24.  for each r_Record in l_MyListUnsorted 
  25.  { 
  26.      // field we want to sort by 
  27.      v_SortingKey = ifnull(r_Record.get("date"),"")
  28.      if(!isNull(v_SortingKey)) 
  29.      { 
  30.          // as this is a date from CRM (atomic date format), arrange so it can be sorted alphabetically in reverse order 
  31.          v_DatePart = v_SortingKey.getPrefix("T").toDate().toString("yyyy-MM-dd")
  32.          v_TimePart = v_SortingKey.getSuffix("T").getPrefix("+").subString(0,5)
  33.          v_SqlDateTime = v_DatePart + " " + v_TimePart; 
  34.          l_SortingKeys.add(v_SqlDateTime)
  35.          m_UnsortedRecords.put(v_SqlDateTime, r_Record)
  36.      } 
  37.  } 
  38.  // 
  39.  // now sort list in descending order and action each record 
  40.  l_SortedKeysDesc = l_SortingKeys.sort(false)
  41.  for each v_SortKey in l_SortedKeysDesc 
  42.  { 
  43.      m_ThisRecord = m_UnsortedRecords.get(v_SortKey)
  44.      info m_ThisRecord; 
  45.  } 

Recap
The original sample data l_MyListUnsorted looks something like this in a JSON parser:
copyraw
[
  {
    "name": "Joel",
    "id": 46498,
    "date": "1977-11-14T12:30:00+00:00"
  },
  {
    "name": "Anonymouse",
    "id": 8949,
    "date": "1923-10-16T15:00:00+00:00"
  },
  {
    "name": "Amazing",
    "id": 335448,
    "date": "1997-07-05T23:15:00+00:00"
  }
]
  1.  [ 
  2.    { 
  3.      "name": "Joel", 
  4.      "id": 46498, 
  5.      "date": "1977-11-14T12:30:00+00:00" 
  6.    }, 
  7.    { 
  8.      "name": "Anonymouse", 
  9.      "id": 8949, 
  10.      "date": "1923-10-16T15:00:00+00:00" 
  11.    }, 
  12.    { 
  13.      "name": "Amazing", 
  14.      "id": 335448, 
  15.      "date": "1997-07-05T23:15:00+00:00" 
  16.    } 
  17.  ] 

The newly generated map m_UnsortedRecords looks something like this in a JSON parser:
copyraw
{
  "1977-11-14 12:30": {
    "name": "Joel",
    "id": 46498,
    "date": "1977-11-14T12:30:00+00:00"
  },
  "1923-10-16 15:00": {
    "name": "Anonymouse",
    "id": 8949,
    "date": "1923-10-16T15:00:00+00:00"
  },
  "1997-07-05 23:15": {
    "name": "Amazing",
    "id": 335448,
    "date": "1997-07-05T23:15:00+00:00"
  }
}
  1.  { 
  2.    "1977-11-14 12:30": { 
  3.      "name": "Joel", 
  4.      "id": 46498, 
  5.      "date": "1977-11-14T12:30:00+00:00" 
  6.    }, 
  7.    "1923-10-16 15:00": { 
  8.      "name": "Anonymouse", 
  9.      "id": 8949, 
  10.      "date": "1923-10-16T15:00:00+00:00" 
  11.    }, 
  12.    "1997-07-05 23:15": { 
  13.      "name": "Amazing", 
  14.      "id": 335448, 
  15.      "date": "1997-07-05T23:15:00+00:00" 
  16.    } 
  17.  } 

The list of keys looks like this:
copyraw
1997-07-05 23:15,
1977-11-14 12:30,
1923-10-16 15:00
  1.  1997-07-05 23:15, 
  2.  1977-11-14 12:30, 
  3.  1923-10-16 15:00 

This above snippet of code, loops through the sorted list of keys and retrieves each record as follows:
copyraw
{"name":"Amazing","id":335448,"date":"1997-07-05T23:15:00+00:00"}
{"name":"Joel","id":46498,"date":"1977-11-14T12:30:00+00:00"}
{"name":"Anonymouse","id":8949,"date":"1923-10-16T15:00:00+00:00"}
  1.  {"name":"Amazing","id":335448,"date":"1997-07-05T23:15:00+00:00"} 
  2.  {"name":"Joel","id":46498,"date":"1977-11-14T12:30:00+00:00"} 
  3.  {"name":"Anonymouse","id":8949,"date":"1923-10-16T15:00:00+00:00"} 
Category: Zoho Deluge :: Article: 277

Joes Word Cloud

14t12   mylistunsorted   code   anonymouse   record   data   46498   sample   like   name   16t15   sort   descending   through   joel   looks   order   1997   each   amazing   1977   05t23   1923   following   records   list   zoho   date   keys   want   JoelLipman.Com

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com