Javascript array sort Method problem?

i used beneath code for “mad-maxer-sells-out” but beside have error for missing semicolon it is not sort properly I want to know any one know why.

loop {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var i = 0;
    var coins = this.findItems();
    // Find the closest coin that is gold.
    // Remember that gold coins have a value of 3.
    if (coins){
   coinSort = coins.sort (function(a, b) {
  return a.distanceTo(this) - b.distanceTo(this)});
 
while(i < coinSort.length){
    coin = coinSort[i];
    i++;
    if (coin.value == 3){
    x = coin.pos.x;
    y = coin.pos.y;
    this.moveXY(x, y); 
    }
}
}
}

Usually I fix indentations in JS-Code simply and enclose them in a code block.

This however is a wonderful example why Indentations are IMPORTANT.

If you look in line 11, you can see that there is an semicolon missing. Let’s fix that:

   coinSort = coins.sort (function(a, b) {
  return a.distanceTo(this) - b.distanceTo(this)});

becomes

coinSort = coins.sort (function(a, b){
    return a.distanceTo(this) - b.distanceTo(this)
});

and, oh wonder, there I see a missing semicolon. Here is the corrected and indented code:

loop {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var i = 0;
    var coins = this.findItems();
    // Find the closest coin that is gold.
    // Remember that gold coins have a value of 3.
    if (coins){
        coinSort = coins.sort (function(a, b) {
            return a.distanceTo(this) - b.distanceTo(this);
        });
 
        while(i < coinSort.length){
            coin = coinSort[i];
            i++;
            if (coin.value == 3){
                x = coin.pos.x;
                y = coin.pos.y;
                this.moveXY(x, y); 
            }
        }
    }
}

Despite of that, I’m not 100% certain that passing functions to another function is really working. At least in Python I get problems. But someone else is more qualified to answer this particular question.

1 Like

thanks an now it has error “don’t make function within a loop” and not work again. and what about passing function in function can deal as object? what you suggest ?

Well, the solution to that is given in the error-message:

function sortFunction(a, b) {
    return a.distanceTo(this) - b.distanceTo(this); 
}

loop {
    // code before
    coinSort = coins.sort (sortFunction);
    // code after
}
1 Like

thanks a lot, but i don’t know why is not sort proper this is my code i use say to show
for me the one with distance 24.* is problem other is correct sorted

// Coins here disappear after a few seconds!
// Get all the gold coins before they vanish.
function sortFunction(a, b) {
    return a.distanceTo(this) - b.distanceTo(this); 
}

loop {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var i = 0;
    var coins = this.findItems();
    // Find the closest coin that is gold.
    // Remember that gold coins have a value of 3.
    if (coins){
        coinSort = coins.sort (sortFunction);
        while(i < coinSort.length){
            coin = coinSort[i];
            i++;
    
            aa= this.distanceTo(coin);
            this.say(aa);
            this.say(i);
        }
    }
}

JavaScript is a bit weird. When you make a new function, this refers to that function, not your hero any more. So you can do something like this instead:

var self = this;
function sortFunction(a, b) {
    return a.distanceTo(self) - b.distanceTo(self);
}
2 Likes

thanks all but the problem is not solve do you think i should immigrant to python can u give me some good references for JS

What is your current code, and what is still going wrong?

thanks a lot dear nick. it is actually in 6 coins one of them is not sort appropriately.

// Coins here disappear after a few seconds!
// Get all the gold coins before they vanish.

var self = this;
function sortFunction(a, b) {
    return self.distanceTo(a) - self.distanceTo(b);
}

loop {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var i = 0;
    var coins = this.findItems();
    // Find the closest coin that is gold.
    // Remember that gold coins have a value of 3.
    if (coins){
        coinSort = coins.sort (sortFunction);
        while(i < coinSort.length){
            coin = coinSort[i];
            i++;
            if (coin.value == 3){
                x = coin.pos.x;
                y = coin.pos.y;
                this.moveXY(x, y); 
            }
        }
    }
}

Yup, looks like it straight up doesn’t work. It doesn’t even seem to be calling your sort function.

1 Like

sorting doesnt return anything. it modifies the original object.

so

coins.sort(sortFunction);

is all you need. then your coins object should be sorted in whatever way your sorting method sorts it.

if that doesn’t work then it has something to do with trying to call those methods in your sort function. in my sort function i instead pre populate my coins array by tacking the distance on to each coin as a property then inside my coin sort method i do it like this.

function coin_sort(a, b) {  
    return a.distance - b.distance;  
}
2 Likes

Hi there friend, I know this thread is a bit old but I have to ask, why you think it’s “weird” that JavaScript changes what “this” refers to in the definition of a function? I associate the word “this” with the currently scoped block of code. I have a programming background in C# mostly. Maybe, that’s why I don’t find it odd or unusual. In fact, It seems perfectly logical and natural to me.

I actually think it’s weird that JavaScript doesn’t properly change the context of “this” to the local function scope when a function is actually executing. “this” should refer to the function itself when it is executing since it is just another object.

For a long-term programmer this might seem logical, but view it from the side of a newcomer.

this.moveXY(1, 1);      // "Global" this

this.foo = function(){
    this.moveXY(1, 2);  //Also "global" this
};

var bar = function(){
    this.moveXY(1, 3);  //Suddenly not global anymore?!?
};

The way the code is displayed suggests it is all one big context. In fact, up until the functions using this all over the place was perfectly save. Now, without any warning (literally spoken) you need to wrap your brain around the problem that this != this, depending on whether you write it inside the curly brackets or not.

I think it falls in the same area as the indentations in Python. Once you are used to them it is perfectly clear why you need them, but util then it is better guessing whether I should place 4, 8 or maybe 12 Spaces in front of this to magically get it “inside” the if, whatever that means.

It really isn’t working. See these discussions:

See my post about how to get past the limitations of callbacks: