# How to sort array in postman?

**URL:** <https://community.postman.com/t/how-to-sort-array-in-postman/7762>\
**Category:** Help Hub\
**Tags:** json, array\
**Created:** [September 13, 2019, 4:54am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762 "2019-09-13T04:54:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![e.mikhailov](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@e.mikhailov](https://community.postman.com/u/e.mikhailov)\
**Post date:** [September 13, 2019, 4:54am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/1 "2019-09-13T04:54:34Z")

</div>

By the way, I have two questions about my code - how to sort an array in Postman and if there is a better way to compare objects except mine. I use this block to compare properties to check equality of JSON objects body1 and body2 (I’m interested in equality of particular set of properties, f. e. body1 can have property “id” and body2 not, so it’s not about strict equality).

```
properties_list.forEach((property) => 
            (Array.isArray(body2[property])) ?
                (pm.expect((body1[property]).sort()).to.deep.equal((body2[property])).sort()) :
                (pm.expect(body1).to.have.deep.property(property, body2[property])));

```

But it seems that chai doesn’t support .sort()  
Other ways to compare doesn’t work for me:

```
pm.expect(body1).to.have.deep.property(property, body2[property])); // or
pm.expect(body1[property]).to.deep.equal(body2[property]));

```

That ways doesn’t provide equality check in any order ([1, 2] will be not equal [2, 1]).

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 13, 2019, 7:39am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/2 "2019-09-13T07:39:09Z")

</div>

Hi @e.mikhailov, could you share an example of the response that you’re trying to compare/test?

---

<div class="post-metadata">

**Author:** ![e.mikhailov](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@e.mikhailov](https://community.postman.com/u/e.mikhailov)\
**Post date:** [September 13, 2019, 7:54am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/3 "2019-09-13T07:54:23Z")

</div>

@singhsivcan Yes, sure, but I can’t share request, so I will try to provide all code that needed.  
Declaring of test function:

```
pm.environment.set("tests", function tests() {
    var tests = {},
    // technically it is not equality but check that body1 *contains* listed properties equal to body2 properties
    tests.equals = function equals(body1, body2, properties_list) {
        properties_list.forEach((property) => 
            pm.expect(body1).to.have.deep.property(property, body2[property])); //doesn't work for arrays
    };

    return tests;

} + '; tests();');

```

Calling of check:

```
var tests = eval(pm.environment.get("tests")),
    body1 = {
        "arr": [
            1,
            2,
            3
        ]
    },
    body2 = {
        "arr": [
            2,
            1,
            3
        ]
    };

tests.equals(body1, body2, ["arr"]);

```

Result:  
 ![deepequalserror](https://us1.discourse-cdn.com/getpostman/original/2X/c/cea790268ec2508a221b82691045be06b94e125e.png)

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 13, 2019, 8:09am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/4 "2019-09-13T08:09:03Z")

</div>

Why not keep it simple and make another function for `arrayTest` instead of reusing the one for object?

```javascript
function checkArr(body1, body2) {
  let arr1 = body1.arr.sort(), //javascript has this in-built for arrays
    arr2 = body2.arr.sort();

  pm.expect(arr1).to.eql(arr2);
}

```

---

<div class="post-metadata">

**Author:** ![e.mikhailov](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@e.mikhailov](https://community.postman.com/u/e.mikhailov)\
**Post date:** [September 13, 2019, 8:35am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/5 "2019-09-13T08:35:37Z")

</div>

Thanks, that works! I’ve tried the same solution but without separate function to check and I seem to mistake somewhere. Your way works fine and I will check for mistakes in my similar solution.

Maybe, it was wrong to call `body1[property].sort()` or `(body1[property]).sort()` like I tried. Maybe, that was the matter.

---

<div class="post-metadata">

**Author:** ![anyt80937](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anyt80937](https://community.postman.com/u/anyt80937)\
**Post date:** [May 13, 2020, 5:43am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/6 "2020-05-13T05:43:47Z")

</div>

hi sivcan  
i want to get a date from an json response rows and sort it either descending or ascending?  
Any idea if this is feasible in postman?

---

<div class="post-metadata">

**Author:** ![anyt80937](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anyt80937](https://community.postman.com/u/anyt80937)\
**Post date:** [May 13, 2020, 6:15am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/7 "2020-05-13T06:15:05Z")

</div>

please ignore sivcan. I got the solution

---

<div class="post-metadata">

**Author:** ![awdsrinivas](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@awdsrinivas](https://community.postman.com/u/awdsrinivas)\
**Post date:** [January 3, 2025, 5:10am UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/8 "2025-01-03T05:10:23Z")

</div>

Can you please share the solution

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 3, 2025, 7:31pm UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/9 "2025-01-03T19:31:31Z")

</div>

Hey @awdsrinivas 👋🏻

Welcome to the Postman Community :postman:

As you’re responding to a thread that is over 5 years old, without providing any context about your particular issue, I’m going to ask that you create a new topic and provide those details so that people have the opportunity to help you.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 3, 2025, 7:31pm UTC](https://community.postman.com/t/how-to-sort-array-in-postman/7762/10 "2025-01-03T19:31:35Z")

</div>


