I would like to sort my collection API based on name, rather than it’s current default.
I am importing from an export in OpenAPI3 from AWS ApiGateway and the list is completely out of order.
I find it hard to get to the API I am looking for when there are many endpoints.
Thanks!
Hi @purringpigeon
I have just written a real ‘hacky’ way of doing it;
Export the collection you want to sort and copy the raw JSON.
Then paste/save this JSON into a variable;
Create a POST request using https://postman-echo.com/post
And in Body > Raw enter your JSON variable name;

In your pre-req script, put this code;
const jsonResponse = JSON.parse(pm.collectionVariables.get("json"));
console.log(jsonResponse);
let x = jsonResponse.item;
x.sort((a, b) => {
let fa = a.name.toLowerCase(),
fb = b.name.toLowerCase();
if (fa < fb) {
return -1;
}
if (fa > fb) {
return 1;
}
return 0;
});
let y = JSON.stringify(x);
pm.collectionVariables.set("myJSON",y);
Then in the ‘Tests’ tab, build the new JSON structure like this;
const jsonResponse = JSON.parse(pm.collectionVariables.get("json"));
let x = "{ \"info\": " + JSON.stringify(jsonResponse.info) +
" , \"item\": " + JSON.stringify(pm.response.json().data) +
", \"event\": "+ JSON.stringify(jsonResponse.event) +
", \"variable\": "+ JSON.stringify(jsonResponse.variable) + " }"
console.log(x);
This will output to console like so;
Then Import as ‘Raw text’;
it should recognise it as a collection;
Before;

After;

Note; This is for a basic collection. If yours has folders/subfolders you may need to alter the code in the ‘Tests’ tab.