Sort/OrderBy values in Array.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2019 12:58 PM
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')].
Thank you for your assistance.
-Wesley
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2019 08:00 AM
Thanks Chuck,
Made change your change with no impact and still get the un-alphabetized list.
-Wesley
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2019 05:36 AM
you are creating an array of json objects which you will not be able to sort.
create an array of comma separated strings and you will be able to sort.
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 appProperty = {};
var appProperty = '';
var sysProperty = new GlideRecord('sys_properties');
sysProperty.addQuery('sys_id',sysCategory.property);
sysProperty.query();
while (sysProperty.next()) {
appProperty = sysProperty.getDisplayValue('description') + ',' + sysProperty.getDisplayValue('name');
}
//var sortList = sysProperty.orderby('appProperty.groupName'); //Failure to sort
//data.appProperties.push(sortList);
data_appProperties.push(appProperty);
}
gs.info(data_appProperties);
data_appProperties.sort();
gs.info('*********************************************');
gs.info(data_appProperties);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2019 05:55 AM
The first point made was to have the database do the sorting using the orderBy() method. When possible, it's always more efficient to have the database do the work. I'd be more interested in getting to the bottom of the "it doesn't work" statement by running the script debugger over the records retrieved when ordering that inner query by description.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2019 06:40 AM
the inner query is doing its job -- the inner query is inside the outer query
so, we get an ordered list of sys_properties (by name or description) FOR EACH outer query record (m2m table)
so the end result is NOT going to be in name or description order
looks to me like you HAVE to order the array AFTER it is completed by array.sort()
and I believe you would have to use strings to sort (sort on a json object property.value possible)?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2019 08:04 AM
Hi GGG,
Here is the output to the log file and sorting didn't occur.
DAS-ChatOps (P),cmdb_ci_service.chatops,
DAS-IAMMob (P),cmdb_ci_service.iammob,
DAS-ContReg (P),das.cmdb_ci_service.contreg,
DAS-Mail (P),das.cmdb_ci_service.mail,
Helix Forecaster API (P),cmdb_ci_service.helix_forecaster_api,
Helix Turbulence API (P),cmdb_ci_service.helix_turbulance_api,
DAS-RDG (P),cmdb_ci_service.rdg,
Bifrost Commands API (P),cmdb_ci_service.bifrost_commands_api,
Bifrost Storage API (P),cmdb_ci_service.bifrost_storage_api,
DAS-Egate (P),cmdb_ci_service.egate
Any other ideas?
-Wesley