- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2020 11:21 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2020 12:36 PM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2020 11:43 AM
Add .sort() to your array. i.e. nameArray.sort() and it should be in alphabetical order.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2020 11:51 AM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2020 11:56 AM
You can try something like this.
https://gomakethings.com/sorting-an-array-by-multiple-criteria-with-vanilla-javascript/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2020 12:18 PM
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 }
];