How to perform sorting in nested Json array

Hey @material-specialist1 :wave: Welcome to the Postman Community :tada:

I have tested with the following script and it seems to return an expected result :wink:

const lodash = require('lodash')

const array = {
        'abc': [
            {'dc': 'example1',
            'item': [
                {'iCode': 1},
                {'iCode': 2},
                {'iCode': 3},                
            ]},
            {'dc': 'example2',
            'item': [
                {'iCode': 'a'},
                {'iCode': 'b'},
                {'iCode': 'c'},                
            ]},
            {'dc': 'example3',
            'item': [
                {'iCode': 'x'},
                {'iCode': 'z'},
                {'iCode': 'y'},                
            ]}
        ]
}

pm.test('iCode are in sorted order', () => {
    for (var i = 0; i < array.abc.length; i++) {
        const itemArray = lodash.map(array.abc[i].item,'iCode')
        const orderedItemArray = lodash.orderBy(itemArray)
        pm.expect(itemArray).to.eql(orderedItemArray)
        console.log(orderedItemArray)
    }

})

Hope this helps! :slightly_smiling_face:

2 Likes