Jump to content

array.sort()


Sansespere

Recommended Posts

Hi I made an array, and I want it's contents to be randomized when the window loads. I'm not quite sure how the relation between a and b determines how it is sorted, but I read up that to do it randomly you make a function like this:

function randomsort() {return 0.5 - Math.random();}

That makes the number 50% negative, and 50% positive? But what if it is 0, then no sorting is done? Did the people who made this function take that into account? To be honest I am not quite sure yet how the a and b and compared in order to sort the array no matter how many things I have read about it. EDIT: I determined that if Math.random finds a number 0 <= x < 1. This means that it has an infinitely small chance more to be positive? I really have a feeling that this is somehow incorrect. (The function AND my thoughts! lol)So the rest of my code looks like this:

function randomize() {arr.sort(randomsort());}

The only problem with this is I am telling it to randomize the array, but not to write it. I don't want it to be written, I want to take numbers from an already randomized array. I do not want it to be only displayed randomly. Is there a way to do this, or is this doing it and my syntax is incorrect? EDIT: I determined that this works when I sort it alphabetically so, it is just my syntax but I don't see any mistakes?Also if anyone with the insight would be kind enough to explain how the sort() function compares two variables to determine how the array is sorted, that would be much appreciated.Thanks in advanced,

Link to comment
Share on other sites

Hi Eru, I tested the a and b thing, I think that, it tells the function to sort the arrays to point a to point b, just like lowest to high.If I write b - a, it will return high to low.Though I'm not sure, I will get back to you if I find the answers.Thanks.

Link to comment
Share on other sites

Thanks Mark. I really wanted to know how a negative number, zero, or a positive number in the sort function effects the way they are ordered. Ok, a - b is low to high and b - a is high to low, but how does the script determine that. Are the index values subtracted or what?And also, my random function is currently not working. So if you see something funny with that just let me know.Thanks,

Link to comment
Share on other sites

perhaps a step-by-step exam of what happens during a sort will shed some light on the process:

var r = [2,3,1,4,2]var buff="", i =1;var r2 = r.sort(function( a , b ){var resp = a-b;  buff+= "#"+(i++)+"- a= "+a+", b="+b+", result="+resp+"\n";return resp;});alert("orig:2,3,1,4,2\nresult:"+r2.join(",")+"\n\n"+buff);

output: orig:2,3,1,4,2result:1,2,2,3,4

  • #1- a= 2, b=3, result=-1
  • #2- a= 3, b=1, result=2
  • #3- a= 2, b=1, result=1
  • #4- a= 3, b=4, result=-1
  • #5- a= 4, b=2, result=2
  • #6- a= 1, b=2, result=-1
  • #7- a= 2, b=2, result=0
  • #8- a= 3, b=2, result=1

Link to comment
Share on other sites

So in other words this is what is happening:[2,3,1,4,2] * #1- a= 2, b=3, result=-1 - (negative number result means that a comes before b ) New Array order:[2,3,1,4,2] * #2- a= 3, b=1, result=2 - (positive number result means that b comes before a) New order:[2,1,3,4,2] * #3- a= 2, b=1, result=1 - (positive means before) new order:[1,2,3,4,2] * #4- a= 3, b=4, result=-1 - (negative means that a comes before b ) new order:[1,2,3,4,2] * #5- a= 4, b=2, result=2 - ( positive b means b before a) new order:[1,2,3,2,4] * #6- a= 1, b=2, result=-1 - (negative means a before b ) new order:[1,2,3,2,4] * #7- a= 2, b=2, result=0 - (no sorting) same order:[1,2,3,2,4] * #8- a= 3, b=2, result=1 ( positive b before a) new order:[1,2,2,3,4]That helps me understand the numeric aspect. Thanks. Now how about if they are strings instead of numbers? It can't add or subtract them or is each character of the string given a value, such that a=1, b=2...etc.?Also if there are 5 terms that means there are (5-1)! possibilities to match the numbers. 4! is 24 so why does this only match two terms 8 times?

Link to comment
Share on other sites

Remember that every character has an ascii value--or in other words, the computer already understands each character as a numeric value. So the sort routine is about the same. There are simply more steps to sorting a string of characters.

Link to comment
Share on other sites

Ok, thank-you! I have learned a lot here. Now the final question is why isn't this working?

function randsort() {return Number(1 - Math.floor(Math.random()*3));}// ORfunction randsort() {return (0.5 - Math.random())}function shuffle() {	deck.sort(randsort());   }

Link to comment
Share on other sites

In this line:deck.sort(randsort())you are passing sort() the return value of randsort(). That's not what sort() expects. It wants a reference to randsort() so it can call it when it wants to. Change the line to this:deck.sort(randsort)I apologize for not noticing this sooner.

Link to comment
Share on other sites

Ahhh ok. That clears it up. Now my page is working as I want it to. I am just going to experiment some more.Thanks everyone for your help!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...