Sort Problem with User Defined functions

Discussion in 'Server and Client Scripting' started by Lutra, May 21, 2010.

  1. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Basically c doesn't get sorted by a when inside b.


    Code (text):
    1. ({
    2.  
    3. a: function(){
    4.           return (Math.round(Math.random())-0.5);
    5. }
    6.  
    7. ,
    8.  
    9. b: function(){
    10.           var c = new Array ( "name1", "name2", "name3");
    11.           c.sort(this.a());
    12.           sys.sendAll(c);
    13. }
    14.  
    15. ,
    16.  
    17. beforeChatMessage : function (src, message){
    18.          this.b();
    19. }
    20. })
    The alphanumeric order appeared every time.

    SOLUTION : this.a in place of this.a()
     
    Last edited: May 22, 2010
  2. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    Idk why it doesn't work for you.

    Code (text):
    1.  
    2.  compare: function() {
    3.          var randNum = (Math.round(Math.random())-0.5);
    4.          print(randNum);
    5.          return randNum;
    6.  }
    7.  
    8.  ,
    9.  
    10.  b: function() {
    11.      var array = new Array(150,249,250,382,383,384,483,484,487,505);
    12.  
    13.      array.sort(this. compare);
    14.  
    15.      for( x in array) print(array[x]);
    16.  }
    17.  
    Works fine. If you use "c" in the function "b" without passing it as a parameter, make sure it's declared globally (and not with "var" before).
     
  3. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Ok I've elaborated on what the c is and what event the b is read from. I don't know what else there is to say.
     
  4. Akusu

    Akusu Guest

    Is this the code you're running? Because it will crash if you forget a semicolon after the this.b()....
     
  5. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Sorry, coyotte told me the problem was the this.a() (it should be this.a). I probably did have a semi colon. I just typed it in a rush.

    Edit: It doesn't work in the first place with the (), I just didn't notice it before. Damn, wasted a lot of time on this.
     
    Last edited: May 22, 2010
  6. Akusu

    Akusu Guest

    Lol get used to it. The hardest problems in coding usually involve finding the one character you fucked up.
     
  7. Lamperi

    Lamperi I see what you did there

    Joined:
    Apr 25, 2010
    Messages:
    2,647
    Likes Received:
    11
    Please don't use compare function that is random.... that made even microsoft's browser page suck... Compare function should be deterministic

    http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html -- here explained well
    http://en.wikipedia.org/wiki/Fisher–Yates_shuffle --- better random sort here

    some background:
    http://techcrunch.com/2010/02/22/microsoft-ballot-screen/
    http://www.sitepoint.com/blogs/2010/03/07/microsoft-fix-their-non-random-browser-choice-screen/ -- another possible random sort (in javascript)
     
  8. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Well sure it is a weak random sort. But the circumstance in which I'm using it in is a tournament script. So it's likely that you'll have different sets and orders of people will be entered in initially. For small tournaments with a large number of people on the server, people would likely join in such a rush that it could well turn out more random than you expect even if you are usually quick to join the tournament. Not to mention it's dependent on the positions of where the other people join as to who you will face. That sort is also used for each round pairings. I guess with tournaments where the join is a long wait, it could well be less random as certain tournament addicts join first.

    So do you think it's still worth adding the more lengthy code to improve the random sort given the circumstance?
     
  9. Lamperi

    Lamperi I see what you did there

    Joined:
    Apr 25, 2010
    Messages:
    2,647
    Likes Received:
    11
    It may be more code, but it is faster, and in a long run, perhaps more correct. Of course the math.random()-0.5 will work, but I wouldn't do it easy way, but instead learn a proper algorithm. Coding can be learning at the same time.
     
  10. Akusu

    Akusu Guest

    This-um wheel. Re-inventum it we did.
     
  11. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Ok, I'll do Fisher Yates with the Math.random. What rng does Math.random use btw?
     
  12. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168