skip to Main Content

I am new to Jolt.

Please how do I sort the following array in descending alphabetical order

Input :

[
  {
    "status": [
      "ERROR",
      "ERROR",
      "REJECT",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR"
    ]
  }
]

I want status REJECT to be the first object, so the output is:

[
  {
    "status": [
      "REJECT",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR",
      "ERROR"
    ]
  }
]

I have seen the jolt sort operation:

{
  "operation": "sort"
}

But, how to I point it at the status array and also specify I need a descending sort please?

2

Answers


  1. XPath 3.1 can do that:

    [ 
     map { 'lender-status' : array { sort(?1?status?*) => reverse() } }
    ]
    

    Or with

    [ 
     map { 'lender-status' : array:sort(?1?status) => array:reverse() }
    ]
    

    Of course any XPath 3.1 solution can also be used in XSLT 3.0 or XQuery 3.1. I see you removed the xslt tag while I wrote the answer but I will post it as an alternative anyway.

    Login or Signup to reply.
  2. If ascending order was the case, then you can easily match each components of the array with theirselves to make "ERROR" : "ERROR" and "REJECT" : "REJECT" pairs within a shift transformation, and apply sort transformation.

    But, unfortunately there’s no direct method to get the descending order whereas a workaround might be given through reversely indexing the components of the array such as

    [
      { // reform subarrays with repeating components and having keys identical to those repeated components
        // each subarray will be sorted in alphabetical order spontaneously 
        "operation": "shift",
        "spec": {
          "*": {
            "status": {
              "*": {
                "*": {
                  "$": "&3.&[]"
                }
              }
            }
          }
        }
      },
      { // convert arrays to attributes by matching each common key with each component 
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2.&1&"
            }
          }
        }
      },
      { // convert whole content to a unique array back, named "status" 
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "&2"
            }
          }
        }
      },
      { // prepare "key" and "val" arrays to be used within the upcoming specs
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$": "&2.key.&",
              "@": "&2.val"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "sz__": "=size(@(1,key))",
            "sz_": "=divide(@(1,sz__),-1)",
            "sz": "=toInteger(@(1,sz_))",
            "key": {
              "*": "=intSum(@(1,&),@(2,sz))"
            }
          }
        }
      },
      { // get "key" and "val" arrays
        "operation": "shift",
        "spec": {
          "*": {
            "key": {
              "*": {
                "@": "&3.&2"
              }
            },
            "*": "&1.&"
          }
        }
      },
      { // match each components of those arrays among them
        "operation": "shift",
        "spec": {
          "*": {
            "key": {
              "*": {
                "@(2,val[&])": "&3.@(3,key[&])"
              }
            }
          }
        }
      },
      { // sort by keys
        "operation": "sort"
      },
      { // convert attributes back to a unique array 
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "&2"
            }
          }
        }
      }
    ]
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search