Unexpected results sorting array by numeric value

I have the following output from an Array Aggregator. I want to sort it by Open Tickets in ascending order:

[
    {
        "array": [
            {
                "Email": "Michael.Jones@company.com",
                "Open Tickets": "1"
            },
            {
                "Email": "Ray.Smith@company.com",
                "Open Tickets": null
            },
            {
                "Email": "Sue.Adams@company.com",
                "Open Tickets": "2"
            }
        ],
        "__IMTAGGLENGTH__": 3
    }
]

To do so, I’m using a Set Variable module with this expression:

{{sort(7.array; "asc"; "Open Tickets")}}

Yet the result is not sorted:

[
    {
        "sorted": [
            {
                "Email": "Michael.Jones@company.com",
                "Open Tickets": "1"
            },
            {
                "Email": "Ray.Smith@company.com",
                "Open Tickets": null
            },
            {
                "Email": "Sue.Adams@company.com",
                "Open Tickets": "2"
            }
        ]
    }
]

I’m aware that Open Tickets is a string in the Array Aggregator output. However, the Set Variable component says Open Tickets is numeric, so I’d expect sort() to handle it as a number.

Am I missing something obvious here? Thanks in advance!

1 Like

Hi @Tully_Lanter,

This is because null values are not used for sorting, so the placeholder for null values remains unchanged during sorting.

Another workaround for this would be, before sorting, you can set the value of “Open Tickets” to 0 if the value is null.





Best regards,

Msquare Automation
Platinum Partner of Make
@Msquare_Automation

2 Likes

Ah, that makes sense. Nice clean solution. Thank you!

(I’m so accustomed to SQL automatically putting nulls first/last that it didn’t cross my mind to deal with them separately. Much appreciated!)

1 Like

Thank you for your kind feedback.

Best regards,

Msquare Automation
Platinum Partner of Make
@Msquare_Automation