2

I've seen another couple of posts similar to this question, but none work for my scenario and at a lost how to solve this (such as Sort a 2D array by the second value)

I will explain the problem - which I am facing within JavaScript;

I have two arrays, which I am using to create a chart within a 3rd party product. I have the following variables;

label["Stock A", "Stock B", "Stock C", "Stock D"]

value["200", "1000", "900", "800"]

I would like this in a 2D array

sorted["Stock B, 1000","Stock C, 900", "Stock D, 800", "Stock A, 200"]

I want to sort by the numerical value, if possible. The problem is that when I use the solution within the question posted above, it only compares the value with one other value. I need this to go through each value already stored within the array.

I found another really long winded way somewhere last week, but it was sorting values such as 100, 1000 next to each other - which I thought was down to not being integers etc but no such luck. I deleted the code in a rage on Friday.

I was hoping that some kind soul at there could help a new coder. O the values come in as strings so they will need to be converted to ints.

There is an unlimited amount of entries in the array, and once I've sorted these correctly I need to keep the top 10 and then group the others into a group with a total - so I would have 11 entries. However I need to sort before I can tackle that.

Thanks in advance.

1 Answer 1

4

I would like this in a 2D array

sorted["Stock B, 1000","Stock C, 900", "Stock D, 800", "Stock A, 200"]

That's not a 2D array, that's a single dimensional array of strings. This would be a 2D array:

sorted = [["Stock B", 1000], ["Stock C", 900], ["Stock D", 800], ["Stock A", 200]];

...which you can sort by the second value like this:

sorted.sort(function(a, b) {
    var avalue = a[1],
        bvalue = b[1];
    if (avalue < bvalue) {
        return -1;
    }
    if (avalue > bvalue) {
        return 1;
    }
    return 0;
});

But you may be better off with a single-dimensional array of objects:

sorted = [
    {name: "Stock B", quantity: 1000},
    {name: "Stock C", quantity: 900},
    {name: "Stock D", quantity: 800},
    {name: "Stock A", quantity: 200}
];

...which you can sort like this:

sorted.sort(function(a, b) {
    var avalue = a.quantity,
        bvalue = b.quantity;
    if (avalue < bvalue) {
        return -1;
    }
    if (avalue > bvalue) {
        return 1;
    }
    return 0;
});
Sign up to request clarification or add additional context in comments.

3 Comments

@TJ - Thanks for this, can I ask how/ why you use a.quantity? In my example would that be value?
Why not just return (a - b);? The two if statements seem unnecessary.
@ErikE: I'm just so used to writing it that way (e.g., for strings and such). Of course you're quite right, if you're doing numbers, you can do return a.quantity - b.quantity;

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.