1

In reference to this posting; I can sort an Array of JSON Object by 2 fields. And this posting also states:

"To add other columns to sort on, you can add other items in the array comparison."

//THIS WORKS BUT NEEDS TO SORT BY ALL 5 FIELDS..
function sortRecords(a, b){
//note the minus before -cmp, for descending order
// field1 (Y or N), field2 (numeric), field3 (numeric), 
// field4 (Y or N), field5 (Y or N)
return cmp( 
    [-cmp(a.field1, b.field1), cmp(a.field2, b.field2)], 
    [-cmp(b.field1, a.field1), cmp(b.field2, a.field2)]
 );}

//THIS ONLY SORTS BY field1 and field2
function sortRecords(a, b){
//note the minus before -cmp, for descending order
// field1 (Y or N), field2 (numeric), field3 (numeric), 
// field4 (Y or N), field5 (Y or N)
return cmp( 
    [-cmp(a.field1, b.field1), cmp(a.field2, b.field2), cmp(a.field3, b.field3)], 
    [-cmp(b.field1, a.field1), cmp(b.field2, a.field2), cmp(b.field3, a.field3)]
 );}

function cmp(x,y){
return x > y ? 1 : x < y ? -1 : 0;
}


///implementation
data = $(data).sort(sortRecords);

What am I doing wrong here?

Community
  • 1
  • 1
Dan Abel
  • 107
  • 1
  • 10
  • It's like reading greek (which I don't read by the way), there's no meaningful data, just a bunch of function calls and comments? Write a basic example with some numbers, strings or whatever that is understandable for someone who does'nt know what all those functions do, maybe set up a [**jsfiddle**](http://jsfiddle.net) etc. – adeneo Dec 10 '12 at 21:27
  • @adeneo : here is my array of JSON object {"name":"JOE","field1":"Y","field2":3,"field3":79,"field4":"Y","field5":"Y"} {"name":"BEN","field1":"Y","field2":15,"field3":113,"field4":"Y","field5":"N"} {"name":"SUE","field1":"Y","field2":35,"field3":395,"field4":"Y","field5":"Y"} I suppose the initial question is: How can I can I sort an array of JSON Objects by multiple fields? The reason I'm in this position is b/c I got most of the data from 1 data source and the other data from another, I have the array 99% sorted, but I added "field5" which needs to be resorted. – Dan Abel Dec 10 '12 at 21:43

1 Answers1

0

I have been using StackOverflow and I combined several examples of how to sort/order; and this function seems to give me what I am looking for...

given this array of JSON objects as [data].

BEFORE {"name":"JOE","field1":"Y","field2":3,"field3":79,"field4":"Y","field5":"Y"} {"name":"BEN","field1":"Y","field2":15,"field3":113,"field4":"Y","field5":"N"} {"name":"SUE","field1":"Y","field2":35,"field3":395,"field4":"Y","field5":"Y"}

I can call sortRecords() to sort how I want.

AFTER {"name":"JOE","field1":"Y","field2":3,"field3":79,"field4":"Y","field5":"Y"} {"name":"SUE","field1":"Y","field2":35,"field3":395,"field4":"Y","field5":"Y"} {"name":"BEN","field1":"Y","field2":15,"field3":113,"field4":"Y","field5":"N"}

function sortRecords(a, b){
    if (a.field1 < b.field1){
        return 1;
    } else if (a.field1 > b.field1){
        return -1;
    } else if (a.field2 < b.field2){
        return 1;
    } else if (a.field2 > b.field2){
        return -1;
    } else if (a.field3 < b.field3){
        return 1;
    } else if (a.field3 > b.field3){
        return -1;
    } else if (a.field4 < b.field4){
        return -1;
    } else if (a.field4 > b.field4){
        return 1;
    } else if (a.field5 < b.field5){
        return -1;
    } else if (a.field5 > b.field5){
        return 1;
    } else {
        return 0;
    }
}

data = $(data).sort(sortRecords);
Dan Abel
  • 107
  • 1
  • 10