2

I need to sort an array of objects being sent into Mirth. Originally the sorting was done via XSLT (since the inbound data was XML), but with a change (outside of my control) the inbound data was changed to JSON. The original sorting was being done in a destination transformation which I have kept.

Inbound JSON example:

{
  "Id":"100001",
  "Set":
  {
    "unimportantdata1":null,
    "unimportantdata2":null,
    "unimportantdata3":"0001-01-01T00:00:00",
    "unimportantdata4":"0001-01-01T00:00:00",
    "ArrayToSort":[
      {
        "vt":"blah",
        "Num":"2",
        "desc":"dp",
        "Value":["1.1","1.2"],
        "Time":"2020-03-23T02:23:41",
        "blah": { "Name": { "LastName":"ob-ln","Firstname":"ob-fn","MiddleName":"ob-mi","Title":null}}
      },
      {
        "vt":"yadda",
        "Num":"1",
        "desc":"dp",
        "Value":["1.1","1.2"],
        "Time":"2020-03-23T02:23:41",
        "blah": { "Name":{"LastName":"ob-ln","Firstname":"ob-fn","MiddleName":"ob-mi","Title":null}}
      }
   ]}
}

I need the ArrayToSort ordered by the "Num" property ascending.

My questions are:

  1. Is the best place to sort this in the destination channel transformer?
  2. I'm guessing the way to go is via JavaScript, but I'm unsure how to proceed -- suggestions?

Thank you in advance

Ruyut
  • 151
  • 11
P.McSwain
  • 344
  • 4
  • 14
  • Does this answer your question? [Sorting an array of objects by property values](https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values) – agermano Mar 31 '20 at 15:50

2 Answers2

3

Turns out the solution is pretty simple.

In JavaScript, you just need to do the following:

msg['Set']['ArrayToSort'].sort(function(a,b){
    return a["Num"] - b["Num"];
});

logger.info(JSON.stringify(msg));
P.McSwain
  • 344
  • 4
  • 14
0

Sorting can be done, using Array.sort. And can update data

let data = {
  Id: "100001",
  Set: {
    unimportantdata1: null,
    unimportantdata2: null,
    unimportantdata3: "0001-01-01T00:00:00",
    unimportantdata4: "0001-01-01T00:00:00",
    ArrayToSort: [
      {
        vt: "blah",
        Num: "2",
        desc: "dp",
        Value: ["1.1", "1.2"],
        Time: "2020-03-23T02:23:41",
        blah: {
          Name: {
            LastName: "ob-ln",
            Firstname: "ob-fn",
            MiddleName: "ob-mi",
            Title: null
          }
        }
      },
      {
        vt: "yadda",
        Num: "1",
        desc: "dp",
        Value: ["1.1", "1.2"],
        Time: "2020-03-23T02:23:41",
        blah: {
          Name: {
            LastName: "ob-ln",
            Firstname: "ob-fn",
            MiddleName: "ob-mi",
            Title: null
          }
        }
      }
    ]
  }
};
data.Set.ArrayToSort = data.Set.ArrayToSort.sort((x, y) => x.Num - y.Num);
console.log(JSON.stringify(data, null, 2));
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • This is basically the same as the other answer that made it in a little bit earlier. If anyone tries using this solution, you must be running Mirth 3.7 or higher in ES6 mode to get the arrow function support. Mirth also does not support `console.log`. – agermano Mar 25 '20 at 21:26