Basically c doesn't get sorted by a when inside b. Code (text): ({ a: function(){ return (Math.round(Math.random())-0.5); } , b: function(){ var c = new Array ( "name1", "name2", "name3"); c.sort(this.a()); sys.sendAll(c); } , beforeChatMessage : function (src, message){ this.b(); } }) The alphanumeric order appeared every time. SOLUTION : this.a in place of this.a()
Idk why it doesn't work for you. Code (text): compare: function() { var randNum = (Math.round(Math.random())-0.5); print(randNum); return randNum; } , b: function() { var array = new Array(150,249,250,382,383,384,483,484,487,505); array.sort(this. compare); for( x in array) print(array[x]); } 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).
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.
Is this the code you're running? Because it will crash if you forget a semicolon after the this.b()....
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.
Lol get used to it. The hardest problems in coding usually involve finding the one character you fucked up.
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)
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?
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.
http://www.trusteer.com/sites/default/files/Temporary_User_Tracking_in_Major_Browsers.pdf Happy reading :P