8 Replies Latest reply on Oct 6, 2016 9:58 AM by try67

    two factors JSON sorting

    MatLac Level 1

      I found an exellent script example to sorts a JSON according to a specified key:

       

      function sortJSON(data, key, way) {

          return data.sort(function(a, b) {

              var x = a[key]; var y = b[key];

              if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }

              if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }

          });

      }

       

      where:

      data = JSON to sort

      key = the key you want it sorted with

      way = ascending or descending order

       

      My JSON is as follow:

       

      var POL = [

        {cName:"John Doe", cDate: new Date(2005, 4, 16), cPassword:"162151", cTeam:34, cEmail:"john.doe@email.com"},

        {cName:"Jane Doe", cDate: new Date(2005, 4, 16), cPassword:"161974", cTeam:34, cEmail:"jane.doe@email.com"},

        {cName:"Jack Doe", cDate: new Date(2013, 3, 22), cPassword:"167406", cTeam:34, cEmail:"jack.doe@email.com"},

      ...

      ]

       

      I need to sort the JSON by cDate first but supposed there are identical dates, I need to sort these by cPassword

       

      How can I update my script, maybe I need something totally different or maybe is it even possible?

        • 1. Re: two factors JSON sorting
          try67 MVP & Adobe Community Professional

          You can compare date objects directly, using the mathematical operators.

          If the dates are equal, then you can compare the passwords (comparing

          strings is more complicated, though).

          • 2. Re: two factors JSON sorting
            MatLac Level 1

            Thanks try67,

            It will only be dates and numbers that are gonna be sorted altough my example shows strings.

             

            Are you telling me I should create a sortedJSON with the first key and THEN sort it with the second key?  I have a feeling that this will not sort by two factors but rather sort two times adding possibilities for errors?

            • 3. Re: two factors JSON sorting
              try67 MVP & Adobe Community Professional

              No, you need to do it all in a single function.
              Basically it would be something like this:

               

              if (a.cDate==b.cDate) {

                   // compare passwords

              } else {

                   // compare dates

              }

              1 person found this helpful
              • 4. Re: two factors JSON sorting
                MatLac Level 1

                I rewrote the function wtih your hints and added a little test to a button, works like a charm.  thanks.

                 

                function sortJSON2(data, key1, key2, way) {

                    return data.sort(function(a, b) {

                        var x = a[key1]; var y = b[key1];

                  if (a[key1] == b[key1]){

                  var x2 = a[key2]; var y2 = b[key2];

                  if (way === '123' ) { return ((x2 < y2) ? -1 : ((x2 > y2) ? 1 : 0)); }

                  if (way === '321') { return ((x2 > y2) ? -1 : ((x2 < y2) ? 1 : 0)); }

                  }

                  else {

                  if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }

                  if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }

                  }

                    });

                }

                 

                 

                console.clear()

                 

                 

                var POL2 = [

                  {param1:1, param2:2435},

                  {param1:2, param2:12},

                  {param1:3, param2:54},

                  {param1:1, param2:1},

                  {param1:5, param2:55},

                  {param1:1, param2:75476},

                  {param1:7, param2:5},

                  {param1:8, param2:98}

                ]

                var workArray1 = []

                var workArray2 = []

                 

                 

                //sort array

                var sorted1 = sortJSON2(POL2, "param1", "param2", "123")

                for (i in sorted1){

                  workArray1.push(sorted1[i].param1)

                  workArray2.push(sorted1[i].param2)

                }

                //display new order

                console.println(workArray1+"\r"+workArray2+"\r")

                 

                //displays correct order

                1,1,1,2,3,5,7,8

                1,2435,75476,12,54,55,5,98

                • 5. Re: two factors JSON sorting
                  MatLac Level 1

                  I might have been quick on the response because I just applied my solution to the real JSON and it doesn't sort the second factor.  So I inserted an alert in the if statement and found out is is never triggered.

                   

                  So definittely, comparing the two dates returns false, is there something I'm not getting?

                   

                  function sortJSON(data, key1, key2, way) {

                      return data.sort(function(a, b) {

                          var x = a[key1]; var y = b[key1];

                    if (a[key1] == b[key1]){

                    app.alert("test")                                         //never triggers

                    var x2 = a[key2]; var y2 = b[key2];

                    if (way === '123' ) { return ((x2 < y2) ? -1 : ((x2 > y2) ? 1 : 0)); }

                    if (way === '321') { return ((x2 > y2) ? -1 : ((x2 < y2) ? 1 : 0)); }

                    }

                    else {

                    app.alert("test2")                                       //triggers about 72 times

                    if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }

                    if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }

                    }

                      });

                  }

                  • 6. Re: two factors JSON sorting
                    MatLac Level 1

                    also, I inserted

                    console.println(x+"\r"+y+"\r"+(x == y)+"\r\r")

                    ust to display each pair of days.  I found out that some are the same but the still return false when compared

                     

                    here is an output example

                     

                    Mon Jul 06 2009 00:00:00 GMT-0400

                    Mon Jul 06 2009 00:00:00 GMT-0400

                    false

                    • 7. Re: two factors JSON sorting
                      MatLac Level 1

                      Since I am trying to compare dates, it seems I must use the getTime() method of the date object for comparison.  Here is the working script.

                       

                      function sortJSON(data, key1, key2, way) {

                          return data.sort(function(a, b) {

                              var x = a[key1].getTime(); var y = b[key1].getTime();

                        if (x == y){

                        var x2 = a[key2]; var y2 = b[key2];

                        if (way === '123' ) { return ((x2 < y2) ? -1 : ((x2 > y2) ? 1 : 0)); }

                        if (way === '321') { return ((x2 > y2) ? -1 : ((x2 < y2) ? 1 : 0)); }

                        }

                        else {

                        if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }

                        if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }

                        }

                          });

                      }

                      • 8. Re: two factors JSON sorting
                        try67 MVP & Adobe Community Professional

                        It should work even without getTime(), but might be better to include it anyway.