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

@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.

4 Likes