# Possibility to test if my id is sorted in ascending order in my body

**URL:** <https://community.postman.com/t/possibility-to-test-if-my-id-is-sorted-in-ascending-order-in-my-body/9791>\
**Category:** Help Hub\
**Tags:** tests, variables, scripts, testscripts\
**Created:** [December 26, 2019, 9:52am UTC](https://community.postman.com/t/possibility-to-test-if-my-id-is-sorted-in-ascending-order-in-my-body/9791 "2019-12-26T09:52:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexisjah](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@alexisjah](https://community.postman.com/u/alexisjah)\
**Post date:** [December 26, 2019, 9:52am UTC](https://community.postman.com/t/possibility-to-test-if-my-id-is-sorted-in-ascending-order-in-my-body/9791/1 "2019-12-26T09:52:02Z")

</div>

I would like to know if it is possible to test that my id is well sorted in ascending order in my body.

“\_metadata”: {  
“offset”: 0,  
“limit”: 10,  
“total”: 3,  
“page”: {  
“current”: 1,  
“total”: 1  
}  
},  
“data”: [  
{  
“id”: 1,  
“description”: “EN - FARBY-LAKIERY Type 1”,  
“status”: “ENABLED”,  
“departmentId”: 2,  
“subDepartmentId”: 1  
},  
{  
“id”: 2,  
“description”: “EN - FARBY-LAKIERY Type 2”,  
“status”: “ENABLED”,  
“departmentId”: 2,  
“subDepartmentId”: 1  
},  
{  
“id”: 3,  
“description”: “EN - FARBY-LAKIERY Type 3”,  
“status”: “ENABLED”,  
“departmentId”: 2,  
“subDepartmentId”: 1  
}  
]  
}

I would like to create a test script to see that my id is sorted in ascending order.

Thank

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [December 28, 2019, 2:41pm UTC](https://community.postman.com/t/possibility-to-test-if-my-id-is-sorted-in-ascending-order-in-my-body/9791/2 "2019-12-28T14:41:27Z")

</div>

**Using pure JavaScript and broken down in smaller pieces**

```
pm.test("All ids are ascending", () => {
    const response = pm.response.json();

    // Extract all ids in an array
    const ids = response.data.map(item => item.id);
    
    // Check that all are ascending
    function isAscending(a) {
        return a.every((x, i) => {
            return i === 0 || x >= a[i-1];
        });
    }
    
    pm.expect(isAscending(ids)).to.be.true;
});

```

**Simplified version**

```
pm.test("All ids are ascending", () => {
    const response = pm.response.json();

    const isAscending = response.data.map(item => item.id).every((current, index, ids) => index === 0 || current >= ids[index-1]);
    
    pm.expect(isAscending).to.be.true;
});

```

P.S. Test and make sure the tests actually fail when needed.

---

<div class="post-metadata">

**Author:** ![alexisjah](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@alexisjah](https://community.postman.com/u/alexisjah)\
**Post date:** [December 30, 2019, 8:26am UTC](https://community.postman.com/t/possibility-to-test-if-my-id-is-sorted-in-ascending-order-in-my-body/9791/3 "2019-12-30T08:26:32Z")

</div>

> [@vdespa](#):
>
> m.test(“All ids are ascending”, () =\> { const response = pm.response.json(); // Extract all ids in an array const ids = response.data.map(item =\> item.id); // Check that all are ascending function isAscending(a) { return a.every((x, i) =\> { return i === 0 || x \>= a[i-1]; }); } pm.expect(isAscending(ids)).to.be.true; });

Thanks to you ! It works very well. I was able to test that the tests really fail when needed. 😀
