For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at http://ascentbusiness.co.uk/zoho-support-2.  For larger projects, check our bespoke pricing structure and receive dedicated support from our hands-on project consultants and developers at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at http://ascentbusiness.co.uk.

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 :: Article: 738

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

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

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
© 2024 Joel Lipman .com. All Rights Reserved.