1

Hi I see there are loads of examples but none explains what I need done.

I want to create and add items to a 2 Dimensional array and sort dynamically.

Some code I have been messing around with:

var Amount = new Array(new Array());  //MULTI ARRAY
var a = 0; //COUNTER

$("input[id^='AmountSpent']").each(function(){
    Amount[a][a] = [a, $(this).val()]; //THIS IS WHERE I GET STUCK... HOW TO ASSIGN VALUES
    a = a + 1;
});

After that I want to sort the array.

So if the array looks like this:

Amount = [[1,2,3,4],[$200,$300,$100,$600]]

I want to sort highest amount first: $600, $300, $200, $100

Can anyone please help me.


U P D A T E


Using the code i got from Rory(Thanks a lot) I am doing the following:

var amounts = [];    
$("input[id^='AmountSpent']").each(function(i, el){
  amounts.push({ index: i + 1, value: $(el).val() });
});
amounts.sort(function(a, b) {
  if(a.value < b.value) return 1;
  if(a.value > b.value) return -1;
  return 0;
});

To loop through the array I am doing :

for (ii = 0; ii < amounts.length; ++ii) {
console.log(amounts[ii].index + " - " +  amounts[ii]); // 
 }

The Result I get is :

1 - [object object]
2 - [object object]
3 - [object object]

2
  • 1
    If there a specific reason that you need it to be a 2 dimensional array? Why not an array of objects which contains both sets of information? Commented Nov 16, 2012 at 15:26
  • What I need to do is I have a value say a = $900 In the array I want to have the sum of the highest values that make up $900 so basically I want to get the indexes/id's [4,3] to use for another calculation. Commented Nov 16, 2012 at 15:46

1 Answer 1

3

A multidimensional array is probably overkill for this. Personally I'd use an array of objects - assuming you need to store the index at all.

var amounts = [];    
$(".foo").each(function(i, el){
    amounts.push({ index: i + 1, value: $(el).val() });
});

amounts.sort(function(a, b) {
    if(a.value < b.value) return 1;
    if(a.value > b.value) return -1;
    return 0;
});

Example fiddle


Update

Your loop code doesn't access the value property, try this:

for (ii = 0; ii < amounts.length; ++ii) {
    console.log(amounts[ii].index + " - " +  amounts[ii].value);
}
Sign up to request clarification or add additional context in comments.

3 Comments

THANKS Gonna try it now! You are probably right about the overkill but I am not a hardcore programmer, just came across this problem in a small project I am doing. What I need to do is I have a value say a = $900 In the array I want to have the sum of the highest values that make up $900 so basically I want to get the indexes/id's [4,3] to use for another calculation.
When I want to loop through array i get a [object object] error @RoryMcCrossan
@Nev I've updated my answer to show you how to get the value from the loop

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.