
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: ADVANCED
Assumes having taken the class SSNF and has good intermediate to advanced level of knowledge and/or familiarity with Scripting in ServiceNow.
From time-to-time I find that I need to be able to sort object arrays. I'm always having to go to the web and look for a solution. Not always is that solution satisfactory. Then one day I found this REALLY great article on sorting object arrays using JavaScript. It had some ideas in it that would be easily implemented in ServiceNow to provide the needed functionality!
So here are three methods for sorting an object array!
NOTE: All of the below methods can be tested using Scripts - Background (where I played with them), or Fix Scripts (which would be overkill in this case).
METHOD 1: Create a Script Include to contain a prototype sort function that would add to the JavaScript language when called.
This would be the great beginning to a function library if you don't already have one.
NOTE: I am not a big fan of single-letter variables (hate them actually), but this code comes directly from the referenced article so I let it be...for now.
1. Navigate to System Definition > Script Includes and click on the New button.
2. Fill out the Script Include with the following
Name: DynamicObjectSort
Accessible from: All application scopes
Active: checked
Script:
Array.prototype.sortBy = function(p) {
return this.slice(0).sort(function(a,b) {
return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
});
};
3. Click on Submit to save your new function library.
Your script will look like this:
4. Navigate to System Definition > Scripts - Background.
5. Fill in the Run script field with the following test script:
gs.include('DyanmicObjectSort');
try {
var peopleList = [
{first_name: 'Barny', last_name: 'Rubble'},
{first_name: 'George', last_name: 'Jetson'},
{first_name: 'Wilma', last_name: 'Flintstone'},
{first_name: 'Adam', last_name: 'Ant'}
];
peopleList = peopleList.sortBy('last_name');
for (var i=0; i < peopleList.length; i++) {
gs.info('---> {0}\t - {1}', [peopleList[i].first_name, peopleList[i].last_name]);
}
}
catch(err) {
gs.error('---> error: ' + err);
}
Your results will look like this:
*** Script: ---> Adam - Ant
*** Script: ---> Wilma - Flintstone
*** Script: ---> George - Jetson
*** Script: ---> Barny - Rubble
So, that works nicely. Now onto a couple of other methods.
METHOD 2: Utilize the existing JavaScript Array.sort method with a an element sort function.
This one was cool, and could be useful in some cases where you wouldn't want to bring in the prototype: It rubs my strongly typed (non-global) variable fur the wrong way, but hey! This IS JavaScript where everything is global, right?!
var peopleList = [
{first_name: 'Barny', last_name: 'Rubble'},
{first_name: 'George', last_name: 'Jetson'},
{first_name: 'Wilma', last_name: 'Flintstone'},
{first_name: 'Adam', last_name: 'Ant'}
];
peopleList.sort(function(a, b){
return a.last_name == b.last_name ? 0 : +(a.last_name > b.last_name) || -1;
});
for (var i=0; i < peopleList.length; i++) {
gs.info('---> {0}\t - {1}', [peopleList[i].first_name, peopleList[i].last_name]);
}
You will end up with the same results as Method 1:
*** Script: ---> Adam - Ant
*** Script: ---> Wilma - Flintstone
*** Script: ---> George - Jetson
*** Script: ---> Barny - Rubble
METHOD 3: Use the Underscore.js library
Some time ago I wrote on how to incorporate the underscore.js library into the ServiceNow JavaScript language base. Underscore has a built-in function: .sortBy(...) which does the trick nicely!
See: Mini-Lab: Adding Underscore.js Into ServiceNow
gs.include('underscorejs.min');
var peopleList = [
{first_name: 'Barny', last_name: 'Rubble'},
{first_name: 'George', last_name: 'Jetson'},
{first_name: 'Wilma', last_name: 'Flintstone'},
{first_name: 'Adam', last_name: 'Ant'}
];
var sortedPeeps = _.sortBy(peopleList, 'last_name');
for (var i=0; i < sortedPeeps.length; i++) {
gs.info('---> {0}\t - {1}', [sortedPeeps[i].first_name, sortedPeeps[i].last_name]);
}
Again, you will end up with the same results as Method 1 & 2:
*** Script: ---> Adam - Ant
*** Script: ---> Wilma - Flintstone
*** Script: ---> George - Jetson
*** Script: ---> Barny - Rubble
As you can see, there are multiple methods for implementing a sort. I really like the last one best as the Underscore.js library has a LOT of additional functionality for both Server and Client side coding.
Obviously my list is not all-exhaustive, but rather gives you an idea for some of the ways in which useful new functions can be added to the Scripting code base!
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 8-19-2016 08:48 AM
I updated the code and brought the article into alignment with my new formatting standard.
- 4,297 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.