Comparable c#

Hello everyone,

I’ve been away from programming and computers for awhile, but am picking it slowly up again. I started a project with C#, XNA 3.1 and Microsoft Visual Express 2008, but I’m now stumped. And yes, I have been searching around the web and knowledge bases for a few hours. Ok, maybe I just am too tired (been up all night), but I’d really like to get this thing solved.

My problem is as follows: I am building geometry on the fly in XNA, as in calculating vertices and indices and placing them into VertexBuffers and IndexBuffers. Or I would like to. I have much of it done, but now I’d like to use the Sort() method of a List<VertexPositionColorTexture>. But to do that I think I should implement the Comparable method of VertexPositionColorTexture, and I don’t know how.

My code compiles and runs, but bombs on the line where I call Sort() on my List with an unhandled InvalidOperationException.

So, in short, how can I call Sort() on a List<VertexPositionColorTexture> in C#?

Thanks.

Whew, finally made some small progress by finding some right keywords to search. I implemented a delegate function as follows:

tmpV.Sort(delegate(VertexPositionColorTexture v1, VertexPositionColorTexture v2) {
                if (v1.Position.X &gt; v2.Position.X) return 1;
                if (v1.Position.X &lt; v2.Position.X) return -1;
                if (v1.Position.X == v2.Position.X) {
                    if (v1.Position.Y &gt;= v2.Position.Y) return 1; else return -1;
                }

                return 0;
                });

For a millisecond I thought I had it, but alas, no. Now I still crash on the Sort() line with a

System.InvalidOperationException was unhandled
  Message="Failed to compare two elements in the array."
  Source="mscorlib"
  StackTrace:
       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
       at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
       at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
       at System.Collections.Generic.List`1.Sort()
       at KangasTesti.TriangleFlag.buildFlag() in D:\kielet\Microsoft Visual Studio 9.0\MyProjects\WindowsGame1\WindowsGame1\TriangleFlag.cs:line 177
       at KangasTesti.TriangleFlag.Initialize(GraphicsDevice gd) in D:\kielet\Microsoft Visual Studio 9.0\MyProjects\WindowsGame1\WindowsGame1\TriangleFlag.cs:line 43
       at WindowsGame1.Game1.Initialize() in D:\kielet\Microsoft Visual Studio 9.0\MyProjects\WindowsGame1\WindowsGame1\Game1.cs:line 49
       at Microsoft.Xna.Framework.Game.Run()
       at WindowsGame1.Program.Main(String[] args) in D:\kielet\Microsoft Visual Studio 9.0\MyProjects\WindowsGame1\WindowsGame1\Program.cs:line 14
  InnerException: System.ArgumentException
       Message="At least one object must implement IComparable."
       Source="mscorlib"
       StackTrace:
            at System.Collections.Comparer.Compare(Object a, Object b)
            at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
            at System.Collections.Generic.ArraySortHelper`1.SwapIfGreaterWithItems(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
            at System.Collections.Generic.ArraySortHelper`1.QuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer)
            at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
       InnerException: 

Anyone?