title: List sort implementation
body: <p>Have some problems with my list and sorting feature. I create class Variation and Variations. I trade Variations class as collection list of my Variation to separate it. Now when i have create collection i would like to sort by Variation property position, hovever i get error. Can you help me out? Below all necessary information.</p>
  
  <p>Variation class:</p>
  
  <pre><code>Public Class Variation
  
      Property ID As Integer
      Property Name As String
      Property Erstellungsdatum As Date
      Property Position As SByte
  
      Sub New()
      End Sub
  
      Sub New(id As Integer)
          _ID = id
      End Sub
  
      Sub New(id As Integer, name As String)
          _ID = id
          _Name = name
      End Sub
      Sub New(name As String, position As Integer)
          _Name = name
          _Position = position
      End Sub
  
      Sub New(id As Integer, name As String, position As SByte)
          _ID = id
          _Name = name
          _Position = position
      End Sub
      Sub New(id As Integer, name As String, erstellungsdatum As Date)
          _ID = id
          _Name = name
          _Erstellungsdatum = erstellungsdatum
      End Sub
  
  End Class
  </code></pre>
  
  <p>class Variations:</p>
  
  <pre><code>Public Class Variations
      Implements IDisposable
      Implements IComparer(Of Variation)
  
      Public MyVariationsCollection As List(Of Variation)
  
      Sub New()
          MyVariationsCollection = New List(Of Variation)
      End Sub
  
      Public Function AddToCollection(ByVal variation As Variation) As Boolean
          MyVariationsCollection.Add(variation)
          Return True
      End Function
  
      Public Sub RemoveFromCollection(index As Integer)
          If Not IsNothing(MyVariationsCollection.Item(index)) Then
              MyVariationsCollection.RemoveAt(index)
          End If
      End Sub
  
  
      Public Function Compare(x As Variation, y As Variation) As Integer Implements IComparer(Of Variation).Compare
          If x.Position &gt; y.Position Then
              Return 1
          End If
  
          If x.Position &lt; y.Position Then
              Return -1
          End If
          Return 0
      End Function
  
  End Class
  </code></pre>
  
  <p>in the code i am doing:</p>
  
  <pre><code>Private variations As New Variations
   variations.AddToCollection(New BusinessLayer.Variation(Id, Name, StartPosition))
  </code></pre>
  
  <p>now i would like to sort this list by e.g Position which i implemented in Variations and i would like it to be there and not in Variation class as i want trade Variations class as collection of Variation object to separate  if you know what i mean.</p>
  
  <p>however if i do:</p>
  
  <pre><code>_variations.MyVariationsCollection.Sort()
  </code></pre>
  
  <p>then i get error:</p>
  
  <pre><code>     ---------------------------
      myprog
      ---------------------------
      System.InvalidOperationException: 
  You can not compare two elements in the array. ---&gt; System.ArgumentException: At least one object must implement IComparable element
  
         w System.Collections.Comparer.Compare(Object a, Object b)
  
         w System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
  
         w System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
  
         w System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
  
         --- Koniec śladu stosu wyjątków wewnętrznych ---
  
         w System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
  
         w System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
  
         w System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
  
         w Cenea.UserControl1..ctor(Variations variations, Artikel mainArtikel) w C:\Users\Robert\Desktop\Cenea\Cenea\UserControl1.vb:wiersz 26
  
         w Cenea.FrmMain.btnVariationProcees_Click(Object sender, EventArgs e) w C:\Users\Robert\Desktop\Cenea\Cenea\FrmMain.vb:wiersz 450
  
         w System.Windows.Forms.Control.OnClick(EventArgs e)
  
         w DevComponents.DotNetBar.ButtonX.OnClick(EventArgs e)
  
         w DevComponents.DotNetBar.ButtonX.OnMouseUp(MouseEventArgs e)
  
         w System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
  
         w System.Windows.Forms.Control.WndProc(Message&amp; m)
  
         w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  
         w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
  
         w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  
         w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  
         w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  
         w Cenea.ProgStart.main() w C:\Users\Robert\Desktop\Cenea\Cenea\ProgStart.vb:wiersz 14
      ---------------------------
      OK   
      ---------------------------
  </code></pre>
  
  <p><strong>Final solution:</strong></p>
  
  <pre><code>Public Class Variation
      Implements IComparable(Of Variation)
  ...
  
    Public Function CompareTo(pother As Variation) As Integer Implements IComparable(Of Variation).CompareTo
          Return String.Compare(Me.Position, pother.Position)
      End Function
  
  ...
  </code></pre>
  
  <p>Variations :</p>
  
  <pre><code>Imports BusinessLayer
  
  Public Class Variations
      Implements IDisposable
  
      Public collection As List(Of Variation)
  
      Sub New()
          collection = New List(Of Variation)
      End Sub
  
      Public Function AddToCollection(ByVal variation As Variation) As Boolean
          collection.Add(variation)
          Return True
      End Function
  
      Public Sub RemoveFromCollection(index As Integer)
          If Not IsNothing(collection.Item(index)) Then
              collection.RemoveAt(index)
          End If
      End Sub
  
      Public Sub SortByPosition()
          collection.Sort()
      End Sub
  
  #Region "IDisposable Support"
      Private disposedValue As Boolean ' To detect redundant calls
  
      ' IDisposable
      Protected Overridable Sub Dispose(disposing As Boolean)
          If Not Me.disposedValue Then
              If disposing Then
                  ' TODO: dispose managed state (managed objects).
              End If
  
              ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
              ' TODO: set large fields to null.
          End If
          Me.disposedValue = True
      End Sub
  
      ' This code added by Visual Basic to correctly implement the disposable pattern.
      Public Sub Dispose() Implements IDisposable.Dispose
          ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
          Dispose(True)
          GC.SuppressFinalize(Me)
      End Sub
  #End Region
  
  End Class
  </code></pre>