How to check if the response is received in a sort order

I have a Json response with employees information. I want to check if the response is sorted by firstname.
ex:
{
“employees”: [
{
“firstname”: “Scott”,
“lastname”: “Bartels”,
“id”: 12345
},
{
“firstname”: “Aaron”,
“lastname”: “Clare”,
“id”: 77845
},
{
“firstname”: “Victor”,
“lastname”: “Drane”,
“id”: 12775
}
]
}

@Aparna.AA You can make use of lodash which is built right into Postman.

You can put the following script in your Tests script tab:

var _ = require('lodash'),
   // Hardcoding response body for this example. Replace it with your original response body.
    responseBody = {
        "employees": [{
            "firstname": "Scott",
            "lastname": "Bartels",
            "id": 12345
        }, {
            "firstname": "Aaron",
            "lastname": "Clare",
            "id": 77845
        }, {
            "firstname": "Victor",
            "lastname": "Drane",
            "id": 12775
        }]
    };


pm.test('Employee names are in sorted order', () => {
   // Using the orderBy function from lodash 
   // Read docs: https://lodash.com/docs/4.17.10#orderBy
    var expectedSortedOrder = _.orderBy(responseBody.employees, ['firstname'],['asc']);

    pm.expect(responseBody.employees).to.eql(expectedSortedOrder);    
});

The above test will fail because obviously the hardcoded values are not sorted.
Once you’ll replace it with correct values, the test will pass.

2 Likes

Thanks @sivcan. This is very helpful.

How to traverse if there was no name to the object
ex:
[
{
“links”: [{
“rel”: “edit”,
“href”: “xxxxx1”
}],
“id”: “9c94163d4ae2”,
“firstname”: “Noble”,
“lastname”: “Berlin”
},
{
“links”: [{
“rel”: “edit”,
“href”: “xxxxx2”
}],
“id”: “9c94163d4ae2”,
“firstname”: “Humble”,
“lastname”: “Berlin”
},
{
“links”: [{
“rel”: “edit”,
“href”: “xxxxx3”
}],
“id”: “9c94163d4ae2”,
“firstname”: “Bumble”,
“lastname”: “Berlin”
}
]

@Aparna.AA By ‘no name’ to the object, do you mean the employees key is not there?
And the response that was returned is an array?

You can still do that over the response that was returned.
Updated the script for that:

var _ = require('lodash'),

    // Parse the array returned in the response body (It is probably a stringified response)
    responseArray = JSON.parse(responseBody);

pm.test('Employee names are in sorted order', () => {
    // Use the responseArray here
    var expectedSortedOrder = _.orderBy(responseArray, ['firstname'],['asc']);

    pm.expect(responseArray).to.eql(expectedSortedOrder);
});

Thank you Sivcan! :+1:

1 Like

@sivcan - I appreciate your above response. However, I just got a quick question. In order to verify if the sorting is in descending order then should we replace asc with desc? If not, then can you please let me know how to verify if it’s in descending order? Your prompt response will be highly appreciated. Thank you! :slight_smile:

Hi @yeaser.khan, yeah you guessed it right - just replace asc with desc.
Well, it’s a library called lodash that is built into postman and orderBy is a function that belongs to that library.
You can read the documentation here: https://lodash.com/docs/4.17.11#orderBy

1 Like

@sivcan - Thank you so much for your quick response.