Javascript Array.Sort() requires a System.Single data type?

So, when I run the function below in its own script, all alone, it works just fine. But when I run it in a larger script, I get the following error:

ArgumentException: Value is not a System.Single.
System.Single.CompareTo (System.Object value)

function SortAndDeleteDuplicates(array : Array)
{
	if(array.length > 2)
	{
		array.Sort();
		for(var i = 0; i < array.length; i++)
		{
			if(i && array *== array[i-1])*
  •  	{*
    
  •  		array.RemoveAt(i);*
    
  •  	}*
    
  •  }	*
    
  • }*
    }
    Now, I know It’s crashing on array.Sort(); . But when I print that array, nothing seems unusual–it’s telling me, for example, that the array contains the following numbers: 58,52,53,54,63,64,65,74,75,76,85,86,87,70. EDIT: I’ve also had the console print all the numbers individually, and I can’t see any difference between the way they’re represented. i’ve tried multiplying them by floats, and that’s no go.
    I know that a single is a primitive, but I don’t know why the sort function would require such a thing. What error could be causing this ArgumentException?

I think that the proper syntax is:

Array.Sort(newArray);

Anyway, you need to make sur that your array only contains comparable objects (primitives are comparable, but object are not in the general case). Maybe one of the object contained is null or not a primitive.

You should use List instead of the Array class. You won’t have to make your own function to remove duplicates if you do that:

import System.Collections.Generic;
import System.Linq;

function Start () {
	var list = new List.<int>([6, 3, 9, 8, 3, 1, 9]);
	list.Sort();
	list = list.Distinct().ToList(); // Remove duplicates
	for (item in list) print (item);
}

Also, you seem to be confused about reference types. When you do “var newArray = array”, that doesn’t make a copy of “array”. All it does is make a new variable which points to “array”. This means that anything that you do to “newArray” is happening to the array you passed in. That also means there’s no sense making the “newArray” variable at all, nor is there any point making the function return anything. Since an array is a reference type, anything you do to it inside the function is permanent, so to speak. If you want to have both the original array and the sorted array separately, then you’d have to make a new array and copy all the elements over. As mentioned, “var newArray = array” doesn’t do that.