Skip to main content
Inspiring
October 6, 2016
Answered

two factors JSON sorting

  • October 6, 2016
  • 8 replies
  • 885 views

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?

This topic has been closed for replies.
Correct answer try67

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

}

8 replies

Inspiring
October 6, 2016

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)); }

  }

    });

}

Inspiring
October 6, 2016

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

Inspiring
October 6, 2016

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)); }

  }

    });

}

try67
Community Expert
Community Expert
October 6, 2016

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

Inspiring
October 6, 2016

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?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 6, 2016

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

}