JSON Sort

In last prerelease we added JSON sort function to our MBS Xojo Util Plugin.

See Sort method in JSONMBS class

It uses in-place sorting of the elements for arrays and object keys.
Here two examples:

[9,4,3.2,3.1,123]
returns
[3.1, 3.2, 4, 9, 123]

and

{ “last”: 1, “First”: 2, “Test”: 3, “abc”: 4}
returns
{ “abc”: 4, “First”: 2, “last”: 1, “Test”: 3 }

As you see it sorts text case insensitive. If you sort a single number, text or boolean value, we just return it as is.

We hope you enjoy the new functions. Please do not hesitate to contact us with questions.

How does it handle a mixed array, for example:

["z", false, "false", "2.5", 2.5, "2", 1, null]

If two values are compared and both are numeric, we compare number. Otherwise we compare text.
But if needed, we could change it for some edge cases.

[false, null, 1, “2”, “2.5”, 2.5, “false”, “z”]

I guess consistency matters, but sorting, for example, booleans against numbers doesn’t make much sense does it? I mean, is True less than are greater than 3.2? It seems like you should throw an exception if the array is mixed, except for nulls which can be moved to the top or bottom to emulate a SQL sort.

Well, it is a small feature and I assume people sort usually arrays with same data types, e.g. string, number or boolean.

At least consider grouping by types:

Null < Array < Boolean < Number < Object < String

How you’d sort an array of arrays or objects is anyone’s guess. :slight_smile:

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.