# 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:** 1\
**Showing post:** 2

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

---

_[View the full topic](https://community.postman.com/t/possibility-to-test-if-my-id-is-sorted-in-ascending-order-in-my-body/9791)._
