How to check if a string is greater than another

I’m a totally newbie, so apologies in advance if I’m asking for something obvious, but I’ve tried to find an answer unsuccessfully.

I’m getting a JSON response with several items and I can sort these items using a sorting parameter. I would like to test if the items are sorted as expected and I’ve been able to do it for numbers and dates, but I’m unable to do it when the elements I’m comparing are two strings.

I capture consecutive strings in a couple of variables and my assertion is as simple as:


but I get the AssertionError: expected ‘name’ to be a number or a date

Do you have any idea of how to do this comparison in Postman?

Hey @pere.francisco

Welcome to the community! :trophy:

I’m not sure I understand what you’re trying to test here - How are you defining greater than for a string?

I might be wrong here but the only thing that I can think of is the number of characters in a given string?

Hey @dannydainton

Well, my idea is to compare ‘Berta’ with ‘Andreas’, and get a pass because ‘Berta’ > ‘Andreas’. I hope there is a way to do this check.

Greater in terms of what though - Alphabetical order?

You could look into something like this:

When you say you want to check if one string is greater than an other, I assume you mean you want to check the length of one string compared to another.

const shortString = "Pear";
const longerString = "Pineapple"

pm.test("Checking the length of strings", function() {

The above takes our first value “Pear” and compares its length to “Pineapple”
Pear is shorter (it has a shorter length) than Pineapple, so this test correctly fails.

I don’t think I’ve ever had a use-case for your scenario though, other than checking if the string was a specific length, such as checking country codes (GB, SE, DE etc.) or currency codes (GBP, USD, SEK etc.)

Thanks for your answer @pfarrell but what I want to check is if a list of strings is ordered alphabetically.

Researching on the ‘localCompare’ solution proposed by @dannydainton, I’ve found an alternate way to solve my problem using ‘lodash’. The JSON results are presumably sorted by name so I’m comparing the original array with the array sorted by the method orderBy:

var _ = require('lodash');
var itemsArray = JSON.parse(responseBody).items;

pm.test("Items are sorted in ascending order by property 'name'", () => {
   // items are sorted by name in ascending order
   var expectedSortedOrder = _.orderBy(itemsArray, [''],['asc']);

   // Compares the original array, allegedly sorted by name, with the array sorted by name

The code does what I expected it to do, but I’m curious what you think.