0

I wish to sort an array of medals. My first sort returns an array sorted according to the gold medals. I then wish to range those which are having the same gold but silver medals are different (same for bronze). I use the following codes that actually makes me run out of memory. This is my code:

static sort(data) {
    let sorted = data.sort((a, b) => b.medal.gold - a.medal.gold);
    let next, temp, current;
    for (let i = 0; i < sorted.length; i++) {
        current = sorted[i].medal;
        if (sorted[i+1]) next = sorted[i+1].medal;
        if (next) {
            if (current.gold === next.gold) {
                if (current.silver < next.silver) {
                    temp = sorted[i+1];
                    sorted[i+1] = sorted[i];
                    sorted[i] = temp;
                }
                else if (current.silver === next.silver) {
                    if (current.bronze < next.bronze) {
                        temp = sorted[i+1];
                        sorted[i+1] = sorted[i];
                        sorted[i] = temp;
                    }
                }
            }
        }
    }
    return sorted;
}
4
  • 1
    can you please share the array Commented Apr 27, 2017 at 9:29
  • Please update your array data into your question for more detailed. Commented Apr 27, 2017 at 9:29
  • 3
    why do you use static in javascript? Commented Apr 27, 2017 at 9:30
  • an example would be: ` [{ id: 1, medals: {gold: 2, silver: 1, bronze: 1}}, { id: 2, medals: {gold: 2, silver: 4, bronze: 1} }, { id: 3, medals: {gold: 5, silver: 1, bronze: 4} } ]` Commented Apr 27, 2017 at 9:47

2 Answers 2

3

You'll want to improve your compare function so it takes care of that requirement:

data.sort((a, b) => (b.medal.gold - a.medal.gold) 
                 || (b.medal.silver - a.medal.silver) 
                 || (b.medal.bronze - a.medal.bronze) )

And then you don't need the (endless) for loop at all.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to set next to null somewhere, because it keeps the value from the previous iteration and the if(next) is always true. Afterwards the function will always create one more element and add it in the array (sorted[i+1] = sorted[i]) until you run out of memory.

Here is a working example:

var rawData = 
    [{ id: 1, medal: {gold: 2, silver: 1, bronze: 1}}, 
     { id: 2, medal: {gold: 2, silver: 1, bronze: 2} }, 
     { id: 3, medal: {gold: 5, silver: 1, bronze: 4} } ];

function sortData(data) {
    let sorted = data.sort((a, b) => b.medal.gold - a.medal.gold);
    let next, temp, current;
    for (let i = 0; i < sorted.length; i++) {
        next = undefined;
        current = sorted[i].medal;
        if (sorted[i+1]) next = sorted[i+1].medal;
        if (next) {
            if (current.gold === next.gold) {
                if (current.silver < next.silver) {
                    temp = sorted[i+1];
                    sorted[i+1] = sorted[i];
                    sorted[i] = temp;
                }
                else if (current.silver === next.silver) {
                    if (current.bronze < next.bronze) {
                        temp = sorted[i+1];
                        sorted[i+1] = sorted[i];
                        sorted[i] = temp;
                    }
                }
            }
        }
    }
    return sorted;
};

console.log(sortData(rawData))

Please note that in the function you are using medal instead of medals as the data you have provided in one of your comments.

3 Comments

Indeed. Well spotted!
Great. But i am also interested in why the code above does not work.
Eh... it is explained.

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.