Test that the arrays sorted by alphabetical order based on value

Hi friends, I am new to Postman and will appreciate your help.

I am trying to right a test to verify that the response body is filtered in alphabetical order based on “stateAbbr” values, but with NO luck :sweat_smile:

the response:

    "data": {
        "findWebLinkFarms": {
            "type": "linkFarmsByCareType_State",
            "_id": "62d080491141040542f1868b",
            "ASSISTED_LIVING": [
                {
                    "averageRating": 3.84,
                    "averageReviewScoreDisplay": 6.53,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/alaska/kodiak",
                        "title": "Alaska, AK"
                    },
                    "numberOfFacilities": 29,
                    "stateAbbr": "AK"
                },
                {
                    "averageRating": 4.15,
                    "averageReviewScoreDisplay": 7.88,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/alabama/delmar",
                        "title": "Alabama, AL"
                    },
                    "numberOfFacilities": 223,
                    "stateAbbr": "AL"
                },
                {
                    "averageRating": 4.26,
                    "averageReviewScoreDisplay": 7.9,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/arkansas/judsonia",
                        "title": "Arkansas, AR"
                    },
                    "numberOfFacilities": 84,
                    "stateAbbr": "AR"
                },
                {
                    "averageRating": 3.98,
                    "averageReviewScoreDisplay": 7.67,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/arizona/mesa",
                        "title": "Arizona, AZ"
                    },
                    "numberOfFacilities": 632,
                    "stateAbbr": "AZ"
                },
                {
                    "averageRating": 4.07,
                    "averageReviewScoreDisplay": 7.94,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/california/riverside-county",
                        "title": "California, CA"
                    },
                    "numberOfFacilities": 2796,
                    "stateAbbr": "CA"
                },
                {
                    "averageRating": 3.99,
                    "averageReviewScoreDisplay": 7.61,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/colorado/colorado-springs",
                        "title": "Colorado, CO"
                    },
                    "numberOfFacilities": 349,
                    "stateAbbr": "CO"
                },
                {
                    "averageRating": 4.16,
                    "averageReviewScoreDisplay": 8.57,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/connecticut/shelton",
                        "title": "Connecticut, CT"
                    },
                    "numberOfFacilities": 145,
                    "stateAbbr": "CT"
                },
                {
                    "averageRating": 4.01,
                    "averageReviewScoreDisplay": 8.37,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/district-of-columbia/washington",
                        "title": "District Of Columbia, DC"
                    },
                    "numberOfFacilities": 11,
                    "stateAbbr": "DC"
                },
                {
                    "averageRating": 4.04,
                    "averageReviewScoreDisplay": 8.3,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/delaware/hockessin",
                        "title": "Delaware, DE"
                    },
                    "numberOfFacilities": 38,
                    "stateAbbr": "DE"
                },
                {
                    "averageRating": 4,
                    "averageReviewScoreDisplay": 8.12,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/florida/saint-petersburg",
                        "title": "Florida, FL"
                    },
                    "numberOfFacilities": 1582,
                    "stateAbbr": "FL"
                },
                {
                    "averageRating": 4.04,
                    "averageReviewScoreDisplay": 8.07,
                    "city": null,
                    "link": {
                        "relativeUrl": "assisted-living/georgia/cumming",
                        "title": "Georgia, GA"
                    },
                    "numberOfFacilities": 696,
                    "stateAbbr": "GA"
                }.....


Best Regards,
Natalia

Hi @NataliaMerkulova

You could try something like this;

//Parse JSON response
const response = pm.response.json();
//Set pass or fail using a boolean (true or false)
let isAlpha = true;
//Get the list from the parsed response
let list = response.findWebLinkFarms.ASSISTED_LIVING;
//Call the 'compare' function (at the bottom of this script)
list.sort(compare);

//Once the isAlpha bool is set in the function print if the list IS or IS NOT alphabetical.
if (isAlpha === false) {
    console.log("NOT Alphabetical");
}
else{
    console.log("IS Alphabetical");
}

//The function
function compare( a, b ) {
    if ( b.stateAbbr < a.stateAbbr ){
        console.log("Pass: " + b.stateAbbr + " is before " + a.stateAbbr);
    }
    if ( b.stateAbbr > a.stateAbbr ){
        //Prints to console
        console.error("Fail: " + b.stateAbbr + " is after " + a.stateAbbr);
        //If any items are out of order then set to false
        isAlpha = false;
    }
}

Output:
Pass
image
Fail
image

1 Like

Hi @w4dd325,
Thank you so much for your help and comments!!!

You helped a lot!!!

Hey @w4dd325 looks like it is giving an error somehow

can you share a screenshot of your response?

Hi @w4dd325 ,
II attached a screenshot, thank you!

I think this line in my code;

let list = response.findWebLinkFarms.ASSISTED_LIVING;

Should be this instead;

let list = response.data.findWebLinkFarms.ASSISTED_LIVING;

@w4dd325 Thank you!
This one is working fine! Thanks for your help!