When trying to fix performance issues, step 1 is profiling. Humans are bad at guessing where the problem is, so put some profiling in. If you don't have access to a profile library, you can write a little StopWatch class to time each line or function that you're interested in. That being said, I may have noticed some things.
Your first couple of comparisons: Layer and Translation.Z. Is Layer a class or a struct? If Layer is a class, they will only be == if they are pointing to the same instance.
[source]
Layer layer1 = new Layer(Layer.Bottom);
Layer layer2 = new Layer(Layer.Bottom);
if(layer1 == layer2)
Console.WriteLine("This line will not be hit if Layer is a class. They are NOT equal");
[/source]
Same goes for Translation.Z. Sometimes you might think they're the same, but it turns out they aren't.
For your TextureArray's. What that code is saying is this. If they both have a texture[0], see if they are different, otherwise they are equivalent. I suspect this is wrong (but might not be in your case). If one side is null, and the other is not null, that is probably a difference you care about.
[source]
// If we don't have a texture array
if (this.TextureArray[0] == null)
{
// but the other guy does
if (other.TextureArray[0] != null)
return -1; // we're smaller
// Otherwise, we both have a null texture and we're the same. move along.
}
// If we do have a texture array, and the other guy doesn't
else if (other.TextureArray[0] == null)
{
return 1; // we're bigger
}
// else we both have a texture, so we have to check
else if (this.TextureArray[0].Id != other.TextureArray[0].Id)
{
return this.TextureArray[0].Id.CompareTo(other.TextureArray[0].Id);
}
[/source]
Depending on the way you want to sort in this case a null texture might be bigger or smaller. Change the return values if this isn't the case.
And lastly, your BlendMode and ScissorRect seems wrong as well. What this says is: If our blend modes are different, then I'm bigger than the next guy. Things that are sorting on this will swap back and forth each time you Sort your list even if nothing changes. So, let's say you have two blend modes, Opaque and Transparent. The first pass you compare Opaque and Transparent, Opaque will be bigger than Transparent. But call Sort again and Transparent will be bigger than Opaque. Not only that, if you have multiple guys that are sorting on this criteria, Opaque and Transparent will be scattered about randomly.
That last one might be the cause of your performance depending on the sort algorithm in use as the list will never stay sorted.
And like I said before, keep in mind whether or not things are Reference Types (class) or Value Types (structs) as that makes a difference in what you're telling the code to do. For Reference Types you're saying am I referencing the same instance. For Value types you're saying: are the values of these objects equivalent.
http://www.gamedev.net/topic/649248-list-remove/#entry5103729
I hope this helps. But PROFILE IT! :)
- Eck