2

I know that this question has been asked many many times in this community but I hardly see the monitor of my laptop as I'm working for a long time. some help plz.

I want to sort this array of object to get the highest and the lowest value of all the given values that are being inserted to the array automatically.

var catesArrayHolder = new Array();
for(var chartGetter=1; chartGetter <= num; chartGetter++){
    var catesArray = new Array();
    for(var chartGetterArray=1; chartGetterArray <= series; chartGetterArray++){
        idOfInput  = "cate"+chartGetter+"_series"+chartGetterArray;
        values = $("#"+idOfInput).val();
        if(values == ""){
            values = 0;
        }

        catesArray.push(values);
    }
    catesArrayHolder.push(catesArray);
}

this function does NOT work with me... it works with the one-dimensional arrays

catesArrayHolder.sort(function(a,b){return a-b});

I appreciate your help.

Develop Smith
  • 146
  • 1
  • 3
  • 13
  • 3
    `a - b` is only really sensible for numbers. You'll have to define an appropriate ordering function for your objects. In this case, that is actually (and `a` and `b` represent) an array of values. If order of values doesn't matter, sort the array of values first (`cratesArray`) before they are put into `cratesArrayHolder`, to make the ordering over the arrays of values simpler. –  Feb 28 '13 at 03:32
  • This might help you: http://jsfiddle.net/DerekL/Z77Jp/ – Derek 朕會功夫 Feb 28 '13 at 03:40

1 Answers1

2

Edit

To sort on the Sum of the sub arrays you can use:

Example

JS

var twoDArray = [[9,1],[5,3],[8,2]];

twoDArray.sort(function(a, b){
    //sort by sum of sub array
    return eval(a.join('+')) - eval(b.join('+')); 
}); 

alert(JSON.stringify(twoDArray)); 

So your code would look like:

catesArrayHolder.sort(function(a, b){
    return eval(a.join('+')) - eval(b.join('+')); 
}); 

Original

Looks like you're trying to sort a two dimensional array.

You'll need to specify the secondary array position in the sort function.

Example

JS

var twoDArray = [[9,1],[5,3],[4,2]];

twoDArray.sort(function(a, b){
    //Sort by the first value in the sub array
    return a[0] - b[0]; 
}); 

alert(JSON.stringify(twoDArray)); 

twoDArray.sort(function(a, b){
    //sort by the second value in the sub array
    return a[1] - b[1]; 
}); 

alert(JSON.stringify(twoDArray)); 

So to get your code working, you'll need something to the effect of:

var posToSortOn = 0; //position in the sub arrays that dictates sort precedence 
catesArrayHolder.sort(function(a,b){return a[posToSortOn]-b[posToSortOn]});
Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
  • This is very helpful, But I need to get the highest value of all. This is my goal of sorting the array. – Develop Smith Feb 28 '13 at 04:01
  • @DevelopSmith - I'm not sure what you mean by "highest value of all" – Brandon Boone Feb 28 '13 at 04:03
  • I mean that all the values will entered to the array are numeric values. In your example `9` is the highest value in `twoDArray` – Develop Smith Feb 28 '13 at 04:08
  • thanks it works fine. Now I can take some rest. you can read this [article](http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) – Develop Smith Feb 28 '13 at 04:17
  • @DevelopSmith - Good points. If you want to avoid eval, then you'll have to implement a loop to sum the arrays. See: [How to go through an array and add their values](http://stackoverflow.com/q/1230233/402706) – Brandon Boone Feb 28 '13 at 14:11