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

The Machine
Mega Sage

Add .sort() to your array.  i.e. nameArray.sort() and it should be in alphabetical order.

@MB 

 

It works for me. But what if i have multiple objects within an array and i want to sort by name ?

 

Ex:

var parser = JSON.parse(responseBody);

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

for(var i=0;i<length;i++){
var ob = {};
if(parser.locations[i].province != ""){

ob.text = parser.locations[i].country + '-' + parser.locations[i].province;
ob.id = parser.locations[i].id;

nameArray.push(ob);

}

 

I got the output as shown below:

[{"text":"Thailand","id":0},{"text":"Japan","id":1},{"text":"Singapore","id":2},{"text":"Nepal","id":3},{"text":"Malaysia","id":4},{"text":"Canada-British Columbia","id":5},{"text":"Australia-New South Wales","id":6}]

 

Now i want to sort with Text object, can you please help me how to do this ?

@MB Sir,

 

I tried but i am not able to get this.

 

with below example i want to sort by title 

 

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

 

Expected result should be

 

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