x


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[i] == 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?

more ▼

asked Apr 11 '12 at 01:32 PM

Catlard gravatar image

Catlard
527 47 57 62

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 12 '12 at 12:27 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

Thanks for the info, Eric, I'll incorporate that into future programming, and I'll check out the list code. I've updated the code to reflect the fact that it's a reference to the original, and not a copy. But I still don't really understand why I'm getting the error, and why it shouldn't work. Any Ideas?

Apr 12 '12 at 03:02 AM Catlard

Hmm...so, I've been trying to figure out this lists business. I understand where one would use lists and arrays, and all that, although now I'm receiving errors about Pushing to my lists. I can't find anything about list syntax. Is there any listing of the functions that can be performed on a list?

Cheers

Simon

Apr 12 '12 at 03:16 AM Catlard

@sgbraunstein: Er, yeah, I kind of forgot to mention that part.... You added at least one float to the array, and then added at least one element that isn't a float. Sort can only work if everything in the array is the same type. That's another reason you should use List, because if you have List.<int>, it can't contain floats, so you won't ever have this problem.

Apr 12 '12 at 03:19 AM Eric5h5

As for List syntax, http://msdn.microsoft.com/en-us/library/d9hw1as6(v=vs.90).aspx You use Add rather than Push. Unityscript uses an extra "." compared to C#, i.e., List.<int> rather than List<int>.

Apr 12 '12 at 03:40 AM Eric5h5

Thanks, that seems extremely logical. I'll stop trying to make unintentional list puns, as well..RE: " Is there any listing of the functions that can be performed on a list?"

Apr 13 '12 at 01:38 AM Catlard
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 11 '12 at 02:33 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

Hmm...the unity script references seem to indicate that the sort syntax I have is correct. And it does work properly in other scripts.

I've had it print all the elements of the array out individually, and it tells me they're all the same--although there are no decimal places. I'm still not sure why it's happening.

Apr 11 '12 at 11:51 PM Catlard

@Eric5h5 has the correct answer.

Apr 12 '12 at 07:18 AM Kryptos
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1356
x204
x57
x32
x17

asked: Apr 11 '12 at 01:32 PM

Seen: 1031 times

Last Updated: Apr 13 '12 at 01:38 AM