- Home /
Sort only considering last entry in my array
I'm filling up this array with these custom IComparable objects and then trying to sort them. My problem is that when it does the sort it's only considering the last entry in the array. This custom class stuff is a little over my head so any help will be greatly appreciated.
import System;
class PriorityEntry extends System.IComparable { var gD : float; var tD : float; var xC : float; var yC : float; var fR : float; var sR : float;
function CompareTo(other : System.Object) : int
{
if (!(other instanceof PriorityEntry)) return;
var peOther : PriorityEntry = other;
if( gD != peOther.gD ) return gD.CompareTo(peOther.gD);
if( tD != peOther.tD ) return tD.CompareTo(peOther.tD);
if( fR != peOther.fR ) return fR.CompareTo(peOther.fR);
if( sR != peOther.sR ) return sR.CompareTo(peOther.fR);
}
}
sorry, the code formatting is not cooperating with me for some reason.
Answer by qJake · Jul 19, 2010 at 09:52 PM
Wow... there are lots of potential problems here.
First and foremost, read the MSDN documentation on CompareTo:
http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(VS.71).aspx
Now... the potential problems I see are:
if (!(other instanceof PriorityEntry))
I don't know how UnityScript works, but that might have to be "is" instead of "instanceOf".
if (!(other instanceof PriorityEntry)) return;
ICompare.CompareTo always returns an int. Throw an exception if you can't return an int.
A lot of this I'm not sure about. For .NET-intensive things like this (implementing interfaces, overriding methods, type-checking, etc), you really should be using C#, instead of the poorly-implemented UnityScript. This is what your script would look like in C#:
using System;
public class PriorityEntry : IComparable { float gD; float tD; float xC; float yC; float fR; float sR;
public int CompareTo(Object other)
{
if (!(other is PriorityEntry))
{
throw new Exception("Cannot compare!");
}
PriorityEntry peOther = other as PriorityEntry;
if (gD != peOther.gD) return gD.CompareTo(peOther.gD);
if (tD != peOther.tD) return tD.CompareTo(peOther.tD);
if (fR != peOther.fR) return fR.CompareTo(peOther.fR);
if (sR != peOther.sR) return sR.CompareTo(peOther.fR);
// You need to return something here, too, or use an if/else structure.
}
}
The problem that I'm encountering doing it this way is that when I try to refer to this class in another script (var priorityCollector : PriorityEntry;) I get the error that 'PriorityEntry' does not denote a valid type.
Stick the script in the Plugins/ folder, it will compile first.
ah, ok. Sorry to keep co$$anonymous$$g back with follow-up questions (you're being really helpful) but in the script where I'm actually referring to this class and I define a variable of the type 'PriorityEntry' (var priorityCollector : PriorityEntry;) I get the error that this object reference is not set to an instance of this object, and that just makes me scratch my head. This second script is written in java.
UnityScript, not "java" (it's actually javascript, not java, they're completely different things). And you need to create a new instance of your class. I don't know how with UnityScript, but something like var priorityCollector = new PriorityEntry();