Sort/OrderBy values in Array.

Wesley Breshear
Tera Expert

Hello,

Hopefully, this is an easy one to solve.  Just need to sort my array based on the 'sysProperty.getDisplayValue('description');'.  I have looked at few KB articles and tried various methods but since my sorting needs to occur in or after the While loop, I am lost how to sort (orderBy) my array.  This is my 'Server Script' section of a Service Portal Widget.

(function () {
	data.appProperties = [];
	var sysCategory = new GlideRecord('sys_properties_category_m2m');
	sysCategory.addQuery('category','0c14fbf0db20ebc02911e525ca9619f9'); //REST API Business Services
	sysCategory.query();
	while (sysCategory.next()) {
		var appProperty = {};
			var sysProperty = new GlideRecord('sys_properties');
			sysProperty.addQuery('sys_id',sysCategory.property);
			sysProperty.query();
			while (sysProperty.next()) {
				appProperty.groupName = sysProperty.getDisplayValue('description');
				appProperty.propertyValue = sysProperty.getDisplayValue('name');
				//appProperty.orderBy(sysProperty.getDisplayValue('description'));  //Failure to sort
				//appProperty.orderBy('groupName');  //Failure to sort
			}
			//var sortList = sysProperty.orderby('appProperty.groupName');  //Failure to sort
  		    //data.appProperties.push(sortList);

		data.appProperties.push(appProperty);
		}
})();

 

Here is the current look/output.  Need to sort/orderby alphabetically on the Business Service Name column [sysProperty.getDisplayValue('description')].

find_real_file.png

Thank you for your assistance.

-Wesley

 

12 REPLIES 12

surajp
Mega Guru

Try the below script,

 


while (sysCategory.next()) {
  var appProperty = {};
  var sysProperty = new GlideRecord('sys_properties');
  sysProperty.addQuery('sys_id',sysCategory.property);
  sysProperty.orderBy('description');
  sysProperty.query();
  while (sysProperty.next()) {
    appProperty.groupName = sysProperty.getDisplayValue('description');
    appProperty.propertyValue = sysProperty.getDisplayValue('name');

    data.appProperties.push(appProperty);
  }
  
}

I have put the data.appProperties.push(appProperty); inside the while loop so that sorted values are passed to data array.

Hi Surajp,

No success.  Breaks the script and no values display.  I followed Chucks advice above and your solution then works but still not sorting.

Any other ideas?

-Wesley

var data_appProperties = [];
var sysCategory = new GlideRecord('sys_properties_category_m2m');
//sysCategory.addQuery('category','0c14fbf0db20ebc02911e525ca9619f9'); //REST API Business Services
//sysCategory.setLimit(10);
sysCategory.query();
while (sysCategory.next()) {

var sysProperty = new GlideRecord('sys_properties');
sysProperty.addQuery('sys_id',sysCategory.property);
sysProperty.orderBy('description');
sysProperty.query();
while (sysProperty.next()) {
var appProperty;
appProperty = sysProperty.getDisplayValue('description') + ',' + sysProperty.getDisplayValue('name');
data_appProperties.push(appProperty);
}

}
for(var i = 0; i<=data_appProperties.length-1; i++){
gs.info(data_appProperties[i]);
}

data_appProperties.sort()

gs.info('*********************************************');
for(var j = 0; j<=data_appProperties.length-1; j++){
gs.info(data_appProperties[j]);
}

 

//BEFORE SORT

*** Script: Number of security questions required during the password reset request,password_reset.qa.num_reset
*** Script: Require authentication for user signature.,com.snc.assessment.signature_authentication
*** Script: Work notes icon used in Task Activity formatter.,glide.ui.incident_activity.image.work_notes
*** Script: Copy attachments from originating incident,com.snc.incident.copy.attach
*** Script: Escalation rule which applies when Shifts overlap. Setting are - [start: Escalate to outgoing Shift, end: Escalate to incoming Shift, all: Escalate to all Shifts].,com.snc.on_call_rotation.escalation_rule_rota_overlap
*** Script: Show breadcrumbs for the categories associated with items when they are displayed in browse and search listings,glide.sc.show_listing_breadcrumbs
*** Script: Logging level for TaskSLAController (default: Notice),com.snc.sla.task_sla_controller.log
*** Script: When filters are changed the graph will recalculate the layout using the currently selected layout algorithm,glide.ngbsm.filters_run_layout_automatically
*** Script: Maximum number of widgets that can render simultaneously on a responsive dashboard,glide.canvas.grid.widget_render_concurrent_max
*** Script: Show approvers when displaying workflow stages with the Linear renderer.,glide.workflow.renderer.linear.show_approver
*** Script: *********************************************

//AFTER SORT

*** Script: Copy attachments from originating incident,com.snc.incident.copy.attach
*** Script: Escalation rule which applies when Shifts overlap. Setting are - [start: Escalate to outgoing Shift, end: Escalate to incoming Shift, all: Escalate to all Shifts].,com.snc.on_call_rotation.escalation_rule_rota_overlap
*** Script: Logging level for TaskSLAController (default: Notice),com.snc.sla.task_sla_controller.log
*** Script: Maximum number of widgets that can render simultaneously on a responsive dashboard,glide.canvas.grid.widget_render_concurrent_max
*** Script: Number of security questions required during the password reset request,password_reset.qa.num_reset
*** Script: Require authentication for user signature.,com.snc.assessment.signature_authentication
*** Script: Show approvers when displaying workflow stages with the Linear renderer.,glide.workflow.renderer.linear.show_approver
*** Script: Show breadcrumbs for the categories associated with items when they are displayed in browse and search listings,glide.sc.show_listing_breadcrumbs
*** Script: When filters are changed the graph will recalculate the layout using the currently selected layout algorithm,glide.ngbsm.filters_run_layout_automatically
*** Script: Work notes icon used in Task Activity formatter.,glide.ui.incident_activity.image.work_notes

 

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022