Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to sort in alphabetic order

SNOW39
Tera Expert

In my catalog item i have 5 fields, User 1, User 2, User 3, User 4 and User 5.

I need these values in my Workflow, sort them in A to Z format. How to achieve this?

10 REPLIES 10

may be we can try that sumanth but i am wondering what could be wrong in that script as i tried with simple example

var arr=["M","D"];

var sorted = arr.sort();

 

and output is D,M

var arr =[];
var fd1 = current.variables.FND_RecLastName.toString();
var fd2 = current.variables.last_name_2.toString();
var fd3 = current.variables.last_name_3.toString();
var fd4 = current.variables.last_name_4.toString();
var fd5 = current.variables.last_name_5.toString(); 

arr.push(fd1);
arr.push(fd2);
arr.push(fd3);
arr.push(fd4);
arr.push(fd5);

 var sortedArr = arr.sort();

gs.log("sortedArr"+sortedArr);

Can you please try this script

This worked!!

How to access each array object so that i can write code based of ech value

Hello,

you can write like below 

 var sortedArr = arr.sort();
 
var firstElement = sortedArr[0];
var secondElement = sortedArr[1];
var thirdElement = sortedArr[2];
var fourthElement = sortedArr[3];
var fifthElement = sortedArr[4];
OR
for(var i=0;i<sortedArr.length;i++)
{
var elements  = sortedArr[i];
}

Please mark the answer correct if it helped you

 

Hello,

Did my answer help you ?

Please mark it correct if it helps you