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.

2
  • 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. Commented Feb 28, 2013 at 3:32
  • This might help you: jsfiddle.net/DerekL/Z77Jp Commented Feb 28, 2013 at 3:40

1 Answer 1

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]});
Sign up to request clarification or add additional context in comments.

5 Comments

This is very helpful, But I need to get the highest value of all. This is my goal of sorting the array.
@DevelopSmith - I'm not sure what you mean by "highest value of all"
I mean that all the values will entered to the array are numeric values. In your example 9 is the highest value in twoDArray
thanks it works fine. Now I can take some rest. you can read this article
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.