Hello! I want to verify that the data being returned in an array is in ascending order (by first_name). This data will often be new or different, so the test will need to be able to read the response dynamically if that makes sense.
[
{
"id": 15640,
"email": "123abc@gmail.com",
"first_name": "123",
"lastname": "123",
"events_purchased": 1,
"queue_events_entries": 0,
"tickets_purchased": 1,
"events_followed": 0,
"overall_spend": 10.90,
"last_purchase": 1648855932000
},
{
"id": 15020,
"email": "darrel.neal@gmail.com",
"age": 28,
"first_name": "darrel",
"lastname": "neal",
"events_purchased": 1,
"queue_events_entries": 0,
"tickets_purchased": 3,
"events_followed": 0,
"overall_spend": 52.70,
"last_purchase": 1648763935000,
"birth_date": "1994-03-02"
},
{
"id": 15639,
"email": "zelda666@gmail.com",
"first_name": "Zelda",
"lastname": "Zelda",
"events_purchased": 1,
"queue_events_entries": 0,
"tickets_purchased": 1,
"events_followed": 0,
"overall_spend": 10.90,
"last_purchase": 1648855790000
}
]
I tried this test to verify the sorting works, however when I change the sorting parameter in the url to sort by tickets instead of name, it fails:
pm.test("sorting", function () {
//loop array to get all first names
let name = response.map(tool => tool.first_name);
//sort first names and ignore case sensitivity
let names = name.sort((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'}))
console.log(names)
pm.expect(response.first_name)===(names)
});