- Home /
passing a javascript array into a function sorts it?
I have no idea what's going on here, but when I pass a javascript array into a function it sorts it??? I don't want it sorted. What's causing the output to be sorted?
function linreg(xarray,yarray) {
Debug.Log(xarray);
Debug.Log(yarray);
}
x=new Array(1,2,3,4,5,6,7,8,9,10);
y=new Array(10,9,8,7,6,5,4,3,2,1);
output =
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
Thanks,
Dan
Answer by Eric5h5 · Aug 07, 2011 at 06:36 AM
That's not what you actually get for output. Instead, it prints the values you have listed there when declaring the arrays. If you notice, your output has "11" there, which isn't part of the array. I guess you have some more code that's printing those values, because arrays aren't sorted when they're passed into a function.
As an aside, always type variables when creating functions. Also, the convention is to use uppercase for function names and lowercase for variable names, which makes code easier to understand.
function Linreg (xarray : Array, yarray : Array) {
Also, there's rarely any reason to use the JS Array class. It's slow and not type-safe. Use built-in arrays for best speed, or if you need dynamically-sized arrays, use generic List.
The 11 was a typo which I fixed. Thanks for the general tips and speed suggestions. I checked the script independently of the main script it is in and it works. If I change the variable names it works. The main script has many statistics javascript functions I have been porting from html/javascript to unity. $$anonymous$$any of the variables in these other functions have var preceding them. Is there a difference in a var declaration inside a function from javascript/html to unityscript that could be causing problems?
Your answer
Follow this Question
Related Questions
Issues referencing variables between functions 1 Answer
Different way to access class variables? 1 Answer
functions file and NullReferenceException: Object reference not set to an instance of an object 1 Answer
Getting a variable from a GameObject inside a 2d array? 1 Answer
Add a temporary variable to an array 1 Answer