Print

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