For Zoho Services only:


I'm actually part of a bigger team at Ascent Business Solutions where we have support technicians and project consultants. Support is for smaller technical fixes but this can include developments, reports or integrations; depending on the size of the task. Projects are for more time-consuming developments such as revamps of the Zoho Suite of apps or on-site training. The advantage of a team is that if I am out-of-office for a day or so, there is always someone at Ascent Business Solutions who can deal with any queries/issues you may have.

Our support rates can be found and purchased at http://ascentbusiness.co.uk/zoho-support-2. A support bundle doesn't have an expiry date. So whether we can do what you want within the bundle and a year later need further support, if there are minutes left on the bundle then there is no additional charge.

Our project rates for bigger developments can be found at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices and will involve a dedicated project consultant along with developers who will hold your hand through the development process.

If you want help building a solution for one of the Zoho Apps in the Zoho Suite, contact us on 0121 293 8140 (UK) or by email at info@ascentbusiness.co.uk. You can also visit our website at http://ascentbusiness.co.uk.

I regularly build and specialize in 2-way API integrations for Xero, Shopify and eBay.

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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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 - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.