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 array objects in alphabetical order

shaik_irfan
Tera Guru

Hi,

I am gettting an array from the server as shown below, I am trying to pull name from it which i am able to do but i want to fetch in alphabetical order.

 

{ "locations": [ { "coordinates": { "latitude": "15", "longitude": "101" }, "name": "Thailand", "country_code": "TH", "id": 0, "last_updated": "2020-03-21T17:18:19.942964Z", "province": "" }, { "coordinates": { "latitude": "36", "longitude": "138" }, "name": "Japan", "country_code": "JP", "id": 1, "last_updated": "2020-03-21T17:18:19.949480Z", "province": "" }, { "coordinates": { "latitude": "1.2833", "longitude": "103.8333" }, "name": "Singapore", "country_code": "SG", "id": 2, "last_updated": "2020-03-21T17:18:19.953686Z", "province": "" } ] } 

 

===

 

var jsonString = '{ "locations": [ { "coordinates": { "latitude": "15", "longitude": "101" }, "name": "Thailand", "country_code": "TH", "id": 0, "last_updated": "2020-03-21T17:18:19.942964Z", "province": "" }, { "coordinates": { "latitude": "36", "longitude": "138" }, "name": "Japan", "country_code": "JP", "id": 1, "last_updated": "2020-03-21T17:18:19.949480Z", "province": "" }, { "coordinates": { "latitude": "1.2833", "longitude": "103.8333" }, "name": "Singapore", "country_code": "SG", "id": 2, "last_updated": "2020-03-21T17:18:19.953686Z", "province": "" } ] }';

var parser = JSON.parse(jsonString);

var length = parser.locations.length;
var nameArray = [];

for(var i=0;i<length;i++)
nameArray.push(parser.locations[i].name);

gs.info(nameArray);

 

Now i am getting the response as shown below:

 

Thailand,Japan,Singapore

But i want the response in
Japan,Singapore,Thailand


1 ACCEPTED SOLUTION

Works for me.  

var votes = [
	{ title: 'Apple', votes: 1 },
	{ title: 'Milk', votes: 2 },
	{ title: 'Carrot', votes: 3 },
	{ title: 'Banana', votes: 2 }
];

votes = votes.sort(function (vote1, vote2) {

	if (vote1.title > vote2.title) return 1;
	if (vote1.title < vote2.title) return -1;

});

console.log(votes)

View solution in original post

7 REPLIES 7

Works for me.  

var votes = [
	{ title: 'Apple', votes: 1 },
	{ title: 'Milk', votes: 2 },
	{ title: 'Carrot', votes: 3 },
	{ title: 'Banana', votes: 2 }
];

votes = votes.sort(function (vote1, vote2) {

	if (vote1.title > vote2.title) return 1;
	if (vote1.title < vote2.title) return -1;

});

console.log(votes)

How can we achieve this in flow designer using data pills?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

If you want the array containing those names to be sorted; you can sort the array

the array already contains the value you require; just sort it

var jsonString = '{ "locations": [ { "coordinates": { "latitude": "15", "longitude": "101" }, "name": "Thailand", "country_code": "TH", "id": 0, "last_updated": "2020-03-21T17:18:19.942964Z", "province": "" }, { "coordinates": { "latitude": "36", "longitude": "138" }, "name": "Japan", "country_code": "JP", "id": 1, "last_updated": "2020-03-21T17:18:19.949480Z", "province": "" }, { "coordinates": { "latitude": "1.2833", "longitude": "103.8333" }, "name": "Singapore", "country_code": "SG", "id": 2, "last_updated": "2020-03-21T17:18:19.953686Z", "province": "" } ] }';

var parser = JSON.parse(jsonString);

var length = parser.locations.length;
var nameArray = [];

for(var i=0;i<length;i++)
nameArray.push(parser.locations[i].name);

nameArray = nameArray.sort();

gs.info("Sorted array isL " + nameArray);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader