JSON Sort

  1. 5 months ago

    Christian S

    Jun 22 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany
    Edited 5 months ago

    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.

  2. Kem T

    Jun 22 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    How does it handle a mixed array, for example:

    ["z", false, "false", "2.5", 2.5, "2", 1, null]
  3. Christian S

    Jun 22 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    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.

  4. Christian S

    Jun 22 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  5. Kem T

    Jun 24 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  6. Christian S

    Jun 24 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  7. Kem T

    Jun 24 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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. :)

or Sign Up to reply!