Sort Multidimensional Array

Started by
9 comments, last by Durakken 9 years, 11 months ago

I think I see what you're saying now ^.^ I'll attempt it and see what happens. Hopefully it works... and doesn't explode.

Also... Fuuuuuuuusion Ha!

Edit: Yay it worked. Thanks!

Here's the complete code to the page... though I doubt it will be useful for anyone else...


<html>
<body bgcolor="black">

<script>
function compare(a,b) {
  // a and b are rows in Star; as rows in Star
  for ( var depth = 0; depth < a.length && depth < b.length; ++depth ) {
    if ( a[depth] < b[depth] )
      return -1;
    else if ( a[depth] > b[depth] )
      return 1;
  }
  return 0;
}

var LidaSize = 20000;
var Stars = 160 + Math.floor((Math.random() * 40));
var Star = new Array(); 

for (i = 0; i <= Stars; i++) {
	x = Math.floor((Math.random() * LidaSize) + 1);
	y = Math.floor((Math.random() * LidaSize) + 1);
	z = Math.floor((Math.random() * LidaSize) + 1);
	CatPerc = Math.floor((Math.random() * 100) + 1);
	pxx = Math.ceil(x / 250);
	pxy = Math.ceil(y / 250);
	pxz = Math.ceil(z / 250);
	
	if (CatPerc < 77) {
		Category = "M";
		color = "red";
	} else if (CatPerc < 89) {
			Category = "K";
			color = "orange";
	} else if (CatPerc < 97) {
			Category = "G";
			color = "yellow";
	} else if (CatPerc < 101) {
			Category = "F+";
			color = "blue";
	}
	
	Star[i] = new Array();
	Star[i][0] = pxx;
	Star[i][1] = pxy;
	Star[i][2] = pxz;
	Star[i][3] = x;
	Star[i][4] = y;
	Star[i][5] = z;
	Star[i][6] = Category;
	Star[i][7] = color;

}

Star.sort(compare);

i = 0;
while ( i < Stars) {
	document.write("<img src="+Star[i][7]+".gif style ='position:absolute; left:"+Star[i][0]+";top:"+Star[i][1]+"' />");
	i++;
}

document.write("<div style ='position:absolute; top: 100'>");

i = 0;
while ( i < Stars) {
	//document.write("<font color=" +Star[i][7]+ ">Star #" +i+1+ ": " +Star[i][6]+ "(" +Star[i][0]+ ", " +Star[i][1]+ ", " +Star[i][2]+ ") Actual Pos - (" +Star[i][3]+ ", " +Star[i][4]+ ", " +Star[i][5]+ ")</font><br />");
		document.write("<font color=" +Star[i][7]+ ">"+Star[i][6]+ "," +Star[i][0]+ "," +Star[i][1]+ "," +Star[i][2]+ "," +Star[i][3]+ "," +Star[i][4]+ "," +Star[i][5]+ "</font><br />");
	i++;
}

document.write("</div>");
</script>
</body>
</html>

This topic is closed to new replies.

Advertisement